python 如何实现两个目录下不同的文件,并输出不同文件的路径,将其写入txt中
2个回答
展开全部
import os
def os_walker(folder):
"""遍历foler里面的文件"""
path = os.path.abspath(folder)
for root,dirs,files in os.walk(path):
if dirs:
continue
#print root,dirs,files
for f in files:
yield f, os.path.abspath(os.path.join(root,f))
def compare(f1, f2):
""""对比出两个文件夹里面的文件""""
f1_list = {f:p for f,p in os_walker(f1)}
f2_list = {f:p for f,p in os_walker(f2)}
common = {_:f1_list[_] for _ in f1_list if _ in f2_list}
print "common: ", common
f1_specific = {_:f1_list[_] for _ in f1_list if _ not in f2_list}
print "f1_specific", f1_specific
f2_specific = {_:f2_list[_] for _ in f2_list if _ not in f1_list}
print "f2_specific", f2_specific
compare("FOLDER1","FOLDER2")
这个代码有很多局限,比如没考虑子目录等,核心思想是用os.walk. 希望有帮助
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询