返回顶部

收藏

判断两个不同目录下文件是否相同,并输出改变或变更文件的路径至txt

更多
import os
import filecmp
import datetime

filepath1 = {}
filepath2 = {}

start_time = datetime.datetime.now()

def get_file_path_list(path):
    file_path = {}
    for root, dir_list, file_list in os.walk(path):
        for file_name in file_list:
            file_path[(root + '\\' + file_name)] = 1
    return file_path

def get_counter_of_file_path_list(filepathlist):
    count = 0
    for fp in filepathlist:
        if fp != None:
            count += 1
    return count

def write_to_txt(filepathinfo):
    if filepathinfo != None:
        f = open('file_contrast.txt', 'a')
        f.writelines(filepathinfo + '\n')
        f.close()

def get_change_and_less_files_path(beforefilepath, afterfilepath, count):
    for fp1 in beforefilepath:
        temp = 0
        for fp2 in afterfilepath:
            if fp1[3:] == fp2[3:]:
                if filecmp.cmp(fp1, fp2):
                    continue
                else:
                    print 'change file:' + fp1
                    write_to_txt('change file:' + fp1)
            else:
                temp += 1
                if temp == count:
                    print 'less   file:' + fp1
                    write_to_txt('less   file:' + fp1)

def get_more_files_path(beforefilepath, afterfilepath, count):
    for fp2 in afterfilepath:
        temp = 0
        for fp1 in beforefilepath:
            if fp1[3:] == fp2[3:]:
                if filecmp.cmp(fp1, fp2):
                    continue
            else:
                temp += 1
                if temp == count:
                    print 'more   file:' + fp2
                    write_to_txt('more   file:' + fp2)

path1 = r'E:\test\Fre_Data_beforeScale_subframe0_New'
path2 = r'F:\test\Fre_Data_beforeScale_subframe0_New'

filepath1 = get_file_path_list(path1)
filepath2 = get_file_path_list(path2)

count1 = get_counter_of_file_path_list(filepath1)
count2 = get_counter_of_file_path_list(filepath2)

get_change_and_less_files_path(filepath1, filepath2, count2)
get_more_files_path(filepath1, filepath2, count1)

end_time = datetime.datetime.now()

print r'统计时间: %s 秒'  % (end_time - start_time).seconds
print 'Done!'

标签:python

收藏

0人收藏

支持

0

反对

0

发表评论