对于初学者来说我只玩,现在使用Python了约2周,即时通讯比较新的proccessess,我试图创建两个目录与子目录进行比较,并打印出任何改变的脚本。 我读过的文章听到有关使用os.walk走的目录和我已经成功地编写打印的理解的方式在一个目录下的所有文件和子目录的脚本。 我也看到了在这里学会了如何比较两个目录,但只比1档深。
import os
x = 'D:\\xfiles'
y = 'D:\\yfiles'
q= [ filename for filename in x if filename not in y ]
print q
显然,这并不做什么,我想它。 然而,这将列出所有文件和所有目录。
import os
x = 'D:\\xfiles'
x1 = os.walk(x)
for dirName, subdirList, fileList in x1:
print ('Directory: %s' % dirName)
for fname in fileList:
print ('\%s' % fname)
如何将它们结合起来,并得到它的工作?
我猜去将外部程序,如@Robᵩ的意见建议,最好的办法。
使用Python我建议做以下几点:
import os
def fileIsSame(right, left, path):
return os.path.exists (os.path.join(left, path.replace(right, '')));
def compare(right, left):
difference = list();
for root, dirs, files in os.walk(right):
for name in files:
path = os.path.join(root, name);
# check if file is same
if fileIsSame(right, left, path):
if os.path.isdir(path):
# recursively check subdirs
difference.extend(compare(path, left));
else:
# count file as difference
difference.append(path);
return difference;
这种做法缺乏正常fileIsSame
功能将确保文件是通过内容或修改日期相同,一定要正确处理路径(因为我不知道这会变种)。 该算法requres您指定的完整路径。
用法示例:
print (compare(r'c:\test', r'd:\copy_of_test'));
如果第二文件夹是第一,所有的路径中的差异(不同的磁盘字母和文件夹名)拷贝被忽略。 输出将是[]
写聚集您的物品的功能。
import os
def listfiles(path):
files = []
for dirName, subdirList, fileList in os.walk(path):
dir = dirName.replace(path, '')
for fname in fileList:
files.append(os.path.join(dir, fname))
return files
x = listfiles('D:\\xfiles')
y = listfiles('D:\\yfiles')
你可以使用列表理解来提取不在两个目录中的文件。
q = [filename for filename in x if filename not in y]
但是,使用套是更加有效和灵活。
files_only_in_x = set(x) - set(y)
files_only_in_y = set(y) - set(x)
files_only_in_either = set(x) ^ set(y)
files_in_both = set(x) & set(y)
all_files = set(x) | set(y)
import os
def ls(path):
all = []
walked = os.walk(path)
for base, sub_f, files in walked:
for sub in sub_f:
entry = os.path.join(base,sub)
entry = entry[len(path):].strip("\\")
all.append(entry)
for file in files:
entry = os.path.join(base,file)
entry = entry[len(path):].strip("\\")
all.append(entry)
all.sort()
return all
def folder_diff(folder1_path, folder2_path):
folder1_list = ls(folder1_path);
folder2_list = ls(folder2_path);
diff = [item for item in folder1_list if item not in folder2_list]
diff.extend([item for item in folder2_list if item not in folder1_list])
return diff