【问题标题】:need to find only subdirs without the top dir只需要找到没有顶级目录的子目录
【发布时间】:2020-04-05 04:52:42
【问题描述】:

我确定已经有了答案,但我找不到它,因为我真的不太了解 Python 代码。 我需要在目录(C:\01) 中找到所有 subDirs(subDir_path/name),我找到了我理解的代码,但它也打印了 Home dir/path (C:\01) 和 subDir 路径/名称。这是我在这里的第一篇文章。先感谢您。 代码如下:

import os

given_path = 'C:\\01'
for path, dirs, files in os.walk(given_path):
    print ("This is path to:  " + path)
    for f in files:
        print ("These are files:"       +f)
    for d in dirs:
        print("These are Directories:"   + d) 

【问题讨论】:

  • so given_path + d 给你子目录路径...有什么问题?
  • 它还打印了 'C:\\01' 的路径,但我只需要没有顶级目录的子目录。

标签: python subdirectory


【解决方案1】:
from pathlib import Path
dirs = list(map(lambda item: str(item), filter(lambda item: item.is_dir(), Path("c:\\01").rglob("*"))))

【讨论】:

  • 感谢您的回答!我试过了,但它抱怨这部分代码“lamba item”
猜你喜欢
  • 1970-01-01
  • 2011-11-29
  • 2013-05-31
  • 1970-01-01
  • 1970-01-01
  • 2015-01-11
  • 2016-02-29
  • 1970-01-01
相关资源
最近更新 更多