【发布时间】:2018-07-28 12:12:49
【问题描述】:
我有一个像这样的深层子文件夹结构:
a/b/file1.txt
a/b/file1.doc
a/b/file2.txt
a/b/file2.doc
a/c/file3.txt
a/c/file3.doc
a/c/d/file4.txt
a/c/d/file4.doc
我想提取所有的 .txt 和 .doc 文件对 - 例如到一个元组列表中 - 文件名相同,只是文件类型不同。
到目前为止,我想出的最好的是以下看起来效率不高的方法:
files = []
for root, dirs, files in os.walk(path):
for filename in files:
if os.path.isdir(os.path.join(os.path.abspath("."), filename)):
file_list = os.listdir(filename)
file_list_copy = file_list.copy()
#for each in file_list of type .txt
# find .doc of same name in file_list_copy
#add the 2 to tuple nd append to list
【问题讨论】: