【问题标题】:check a directory and its subdirectory and print the path检查目录及其子目录并打印路径
【发布时间】:2018-07-10 12:31:44
【问题描述】:

我想编写一些代码来检查一个目录(及其子目录)是否与给定的字符串匹配,让字符串为New。然后我想打印从根到包含此字符串New 的特定目录的路径。但是,我的代码只正确打印第一级的目录,例如C:\\New Folder,如果里面有一个与字符串匹配的子目录,例如C:\\New Folder\\New Folder,它只会打印为C:\\New Folder。更多详细信息,请查看我的代码片段:

 import os
 rootDir = 'C:\\New Folder'
 os.chdir(rootDir)
 for root, subdirs, files in os.walk(rootDir):
     for dirs in subdirs:
         splitdirs = dirs.split(" ")
         prefix = "New"
         if splitdirs[0] == prefix:
            newDir = os.path.join(rootDir, dirs)
            print(newDir)

所以我想要打印出C:\\New FolderC:\\New Folder\\New Folder,而不是给我两次C:\\New Folder。谁能解释为什么会这样?我是新手,所以可能我在这里缺乏一些编程思维方式。感谢您的检查。

【问题讨论】:

  • os.chdir(rootDir) 是多余的
  • 目录名应该包含给定的前缀还是以它开头?
  • 你可能打错了:在os.path.join(...) 中使用root 而不是rootDir
  • @AzatIbrakov 目录名应该只包含给定的前缀
  • @AzatIbrakov 是的,感谢您指出这一点,不知何故我没有注意到我在那里犯了一个错误

标签: python recursion


【解决方案1】:

我认为您唯一需要更改的是以下行:

newDir = os.path.join(rootDir, dirs)

到:

newDir = os.path.join(root, dirs)

编辑

如果您的搜索词已经存在于您的 rootDir 中,那么您可以将您的代码简化如下:

import os
root_dir = 'C:\\New Folder'
searching_word= "New"
for root, subdirs, files in os.walk(root_dir):
    current_directory = os.path.basename(root)
    if searching_word in current_directory:
        print(root)

【讨论】:

  • 如果“起点”rootDirC:\\New Folder,这个解决方案会打印吗?在我看来,它没有......
  • 是的,你是对的,如果搜索词已经存在于 rootDir 中,它就不会被打印出来。我更新了我对那个案例的回答。
【解决方案2】:

不要查看subdirsos.walk() 递归地工作,所以你可以只使用root。你也不必在那里chdir

import os
root_dir = 'C:\\New Folder'
prefix = "New"
for root, subdirs, files in os.walk(root_dir):
    current_dir = root.split("\\")[-1] #name of current dir
    dirname_splitted = current_dir.split() #split() implies split(" ")
    if dirname_splitted[0] == prefix:
        print(root)

不知道它是否适用于 Windows,但可以在我的 Ubuntu 上使用 root_dir = "/tmp"root.split("/")

编辑:在 cmets 中,您说它应该只包含给定的前缀,而不是以它开头。然后代码变得更简单了:

import os
root_dir = 'C:\\New Folder'
prefix = "New"
for root, subdirs, files in os.walk(root_dir):
    current_dir = root.split("\\")[-1] #name of current dir
    if prefix in current_dir:
        print(root)

【讨论】:

【解决方案3】:

当您定义 newDir 时,您使用的是您的 rootDir,而不是 root。 rootDir 是你的第一个文件夹,而 root 是当前 os.walk 迭代的路径。

你的代码应该是:

import os
rootDir = os.path.join(os.getcwd())
os.chdir(rootDir)
for root, subdirs, files in os.walk(rootDir):
    for dirs in subdirs:
        splitdirs = dirs.split(" ")
        prefix = "New"
        if splitdirs[0] == prefix:
            newDir = os.path.join(root, dirs)
            print(newDir)

【讨论】:

    【解决方案4】:

    你需要这个

    import os
    rootDir = 'D:\\New folder'
    os.chdir(rootDir)
    for root, subdirs, files in os.walk(rootDir):
        for dirs in subdirs:
            splitdirs = dirs.split(" ")
            prefix = "New"
            if splitdirs[0] == prefix:
                newDir = os.path.join(root, dirs)
                print(newDir)
    

    【讨论】:

      猜你喜欢
      • 2017-04-03
      • 2017-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多