【问题标题】:Find a specified folder within a whole partition [closed]在整个分区中查找指定文件夹[关闭]
【发布时间】:2015-10-20 14:28:16
【问题描述】:

我需要找到一个指定的文件夹,该文件夹可以位于磁盘上的任何位置。 我想知道用 Python 3.4 最快的解决方案是什么。

我知道文件夹名称,例如“XXX”,它是子文件夹“YYY”。而且为了不容易,有许多名为“XXX”的文件夹,但没有一个包含“YYY”文件夹。所以它非常独特。

我想在 C: 上找到文件夹“XXX”,如果找到,则检查它是否包含“YYY”。 但也许有某种库可以加快速度或其他什么?

【问题讨论】:

    标签: python python-3.x directory


    【解决方案1】:
    import os
    
    partition = input("Which drive do you want to search? ")
    dirname = "XXX"
    subdirname = "YYY"
    for root, dirs, _fnames in os.walk(partition):
        if os.path.basename(root) != dirname: continue
        if not os.path.isdir(os.path.join(root, subdirname)): continue
        print("Found required folder:", root)
        break
    

    【讨论】:

    • 基本上,os.walk() 进入path中的每个文件夹,然后进入每个文件夹的每个子目录,越来越深?
    • 这就是我想知道的。谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-09
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 2021-06-29
    • 2019-02-28
    相关资源
    最近更新 更多