【问题标题】:how to avoid searching a folder如何避免搜索文件夹
【发布时间】:2021-01-06 01:53:20
【问题描述】:

如何避免搜索文件夹?该脚本会遍历每个文件夹来搜索您的文件,如何避免搜索应用程序?或者只搜索我告诉它的文件夹。我已经尝试了至少 3 个小时

from PIL import Image
user_path = ("/Users/" + getpass.getuser())
FileName = input("file name, please, including the exsention: ")
print("working?")
for folder, sub_folder, files in os.walk(user_path):
    print(f"folder is {folder}")
    for sub_fold in sub_folder:
        print(f"sub folder is {sub_fold}")
        for f in files:
            print(f"file: {f}")
            if FileName == f:
                print("file found")
                print(os.path.abspath(os.path.join(root, name)))

【问题讨论】:

  • 我不确定我是否明白你在问什么。您正在编写一个搜索文件夹中文件的程序,它应该避免搜索文件夹?那么它应该什么都不做?
  • 它在所有文件夹中搜索文件名,但有些文件夹不值得搜索,例如应用程序或库,所以我想排除它们
  • 你的意思是避免搜索不必要的文件夹。
  • 否,避免在不必要的文件夹中搜索
  • for-循环的开头,检查当前folder 是否在一组排除的(您必须事先创建)中。如果是,continue 跳过搜索。

标签: python file path


【解决方案1】:

创建排除文件夹的数组。 当循环进入文件夹时,检查文件夹名称是否在上面创建的数组中。如果只是忽略。

【讨论】:

  • 如何创建一个数组?它只是一个列表吗?如果是这样,那么它不起作用
  • 您使用文件夹名称创建数组/列表,这些名称将被排除在搜索之外。如果文件进入该文件夹(在该文件夹处循环),您检查的是该列表/数组中的文件夹名称。如果是的话,你不会因为子文件夹/文件而被愚弄。
【解决方案2】:

我做了一个示例代码。请检查并回复我。

import os

ext = ["a", "b", "c"] # I assume these are unnecessary folders.
for folder, sub_folder, files in os.walk(user_path):
    print(f"folder is {folder}")
    for sub_fold in sub_folder:
        if sub_fold in ext:
            continue
        else:      
            print(f"sub folder is {sub_fold}")
            for f in files:
                print(f"file: {f}")
                if FileName == f:
                    print("file found")
                    print(os.path.abspath(os.path.join(root, name)))

【讨论】:

    【解决方案3】:

    os.walk 遍历整个目录树,在每次迭代时显示当前目录、其直接子文件夹和其直接文件。只要您是自上而下(默认),您就可以通过从文件夹列表中删除子文件夹来停止对其进行迭代。在此示例中,我将黑名单设置为源中的固定列表,但您可以根据需要提示输入它。在每个文件夹迭代中,您需要做的就是查看想要的文件名是否在该迭代的文件名列表中。

    from PIL import Image
    import getpass
    import os
    
    # blacklist folders paths relative to user_path
    blacklist = ["Applications", "Downloads"]
    
    # get user root and fix blacklist
    # user_path = ("/Users/" + getpass.getuser())
    user_path = os.path.expanduser("~")
    blacklist = [os.path.join(user_path, name) for name in blacklist]
    FileName = input("file name, please, including the exsention: ")
    print("working?")
    for folder, sub_folders, files in os.walk(user_path):
        # eliminate top level folders and their subfolders with inplace
        # remove of subfolders
        if folder in blacklist:
            del sub_folders[:]
            continue
        # in-place remove of blacklisted folders below top level
        for sub_folder in sub_folders[:]:
            if os.path.join(folder, sub_folder) in blacklist:
                sub_folders.remove(sub_folder)
        if FileName in files:
            print("file found")
            print(os.path.abspath(os.path.join(folder, FileName)))
    

    【讨论】:

      猜你喜欢
      • 2010-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 1970-01-01
      • 2010-10-24
      • 2017-09-05
      相关资源
      最近更新 更多