【发布时间】: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