我试图通过遍历文件夹中存在的文件来比较不同文件夹中的两个文件扩展名。
import os
from os.path import splitext
hpath = os.path.dirname(r'C:\Monarch')
dpath = os.path.dirname(r'C:\DOLV')
documents = set([splitext(filename)[0] for filename in dpath])
matches = [filename for filename in set(hpath) if splitext(filename)[0] in documents]
print (matches)
我尝试过此操作,但结果仅为C, : \\
我什至尝试过
from os import walk
hpath = r'C:\Monarch'
dpath=r'C:\DOLV'
h = []
for (dirpath, dirnames, filenames) in walk(hpath):
h.extend(filenames)
break
print(filenames)
d=[]
for (dirpath, dirnames, filenames) in walk(dpath):
d.extend(filenames)
break
print(filenames)
我无法比较它们。我可以打印名称,但无法遍历这些文件名并打印匹配项。
我可以做些什么使其起作用?
关于, 仁。
答案 0 :(得分:0)
解决了我的问题。
from os import walk
hpath = r'C:\Monarch'
dpath=r'C:\DOLV'
h = []
for (dirpath, dirnames, filenames) in walk(hpath):
h.extend(filenames)
break
print(filenames)
d=[]
for (dirpath, dirnames, filenames) in walk(dpath):
d.extend(filenames)
break
print(filenames)
documents = set([splitext(filename)[0] for filename in d])
matches = [filename for filename in set(h) if splitext(filename)[0] in documents]
print (matches)