【问题标题】:how to find file in subfolder's [duplicate]如何在子文件夹的[重复]中查找文件
【发布时间】:2021-11-28 05:05:59
【问题描述】:

如何获取文件路径 我有一个目录

C: / User / Production

它有几个文件夹 (10),其中也有子文件夹。 我需要在其中一个文件夹中找到 test.pdf 文件。

现在我的代码只能在文件夹中注册的文件夹中找到文件。

我的代码

root = 'C:\\User\\Production'
folders = ['ONE','TWO','FREE','FOUR',......]
file = 'test.img'
folders_comtains_file = []
for folder in folders:
     filepath = path.join(root,folder,file)
     if path.isfile(filepath):
         folders_comtains_file.append(filepath)

print(orderName)

【问题讨论】:

  • import os from os import listdir from os.path import isfile, join path = "C:\\Projects" # Your path search_file = "aidan.ovpn" # File to search for files_found = [] for path in [x[0] for x in os.walk(path)]: onlyfiles = [f for f in listdir(path) if isfile(join(path, f))] files_found.append(path) if search_file in onlyfiles else [] print(files_found) # List of paths where file is found

标签: python


【解决方案1】:

我认为您正在寻找 os.walk 方法,它应该可以帮助您找到子目录中的所有文件https://www.tutorialspoint.com/python/os_walk.htm

【讨论】:

    猜你喜欢
    • 2013-11-14
    • 1970-01-01
    • 2014-08-22
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多