加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-05-24 23:49:14 所属栏目:Python 来源:互联网
导读:判断两个不同目录下文件是否相同,并输出改变或变更文件的路径至txt

下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。

脚本之家小编现在分享给大家,也给大家做个参考。

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,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:testFre_Data_beforeScale_subframe0_New'
path2 = r'F:testFre_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,count1)
                                        
end_time = datetime.datetime.now()

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

以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读