xttt05
2017-08-09 11:13
采纳率: 100%
浏览 842
已采纳

py比较文件代码问题求解

#!/usr/bin/python
#-*-coding:utf-8-*-
#python difference_engine_2_os_my_last.py test11 test22
import sys
import os
import hashlib

if len(sys.argv)< 3:
print ("you need to specify two directory ")
print (sys.argv[0],"directory1,directory2")#sys.argv[0] 获取py文件脚本名
sys.exit()
directory1=sys.argv[1]
directory2=sys.argv[2]

print ("comparing:")
print (directory1)
print (directory2)

for directory in [directory1,directory2]:#使用for 循环来对目录1目录2进行同样的操作
if not os.access(directory,os.F_OK):
print (directory,"isn't a valid directory!")
sys.exit()

print ("directoy",directory)

for item in os.walk(directory):#os.walk()遍历目录,这里for循环把directory1,2赋值给directory

print (item)

##print (os.path.join('G:\py\test22\test222','2222.txt'))

def md5(file_path):
"""返回一个md5的值"""
read_file = open(file_path)
the_hash = hashlib.md5()
for line in read_file.readlines():
the_hash.update(line.encode("utf8"))

print ("采集指纹成功,输出:",the_hash.hexdigest())

return the_hash.hexdigest()

def directory_listing(dir_name):
"""返回所有文件的字典"""
dir_file_list = {}
#新建一个字典

dir_root = None
dir_trim =0
for path,dirs,files in os.walk(dir_name):
    if dir_root is None:
        dir_root = path
#找出根目录
    dir_trim = len(dir_root)
print ("键名:",dir_name,)
print ("根目录是:",dir_root)
     #找出根目录

trimmed_path = path[dir_trim:]
if trimmed_path.startswith(os.path.sep):
    tirmmed_path = trimmed_path[1:]
    """列表切片"""
    """"""

for each_file in files:
    file_path = os.path.join(trimmed_path,each_file)
    dir_file_list[file_path] = True
return (dir_file_list,dir_root)

dir1_file_list,dir1_root = directory_listing(directory1)
dir2_file_list,dir2_root = directory_listing(directory2)
results = {}

"""储存结果"""
for file_path in dir2_file_list.keys():
if file_path not in dir1_file_list:
results[file_path] = "没有出现在第一个目录中"
else:
print ("hahaha")
file1 = os.path.join(dir1_root,file_path)
file2 = os.path.join(dir2_root,file_path)
if md5(file1) != md5(file2):
results[file_path] = "这是不同于目录2中的文件"
print ("这是不同于目录2中的文件")
else:
results[file_path] = "这是相同的文件"

for file_path,value in dir1_file_list.items():
if file_path not in results:
results[file_path]= "没有出现在第二个目录中"

print
for file_path,result in sorted(results.items()):
if os.path.sep not in file_path and "same" not in result:
print (path,result)
for path,result in sorted(results.items()):
if os.path.sep in file_path and "same" not in result:
print (path,result)

这是一段比较文件的代码,求解为什么找出相同的文件的代码没有运行,即
else:
print ("hahaha")
file1 = os.path.join(dir1_root,file_path)
file2 = os.path.join(dir2_root,file_path)
if md5(file1) != md5(file2):
results[file_path] = "这是不同于目录2中的文件"
print ("这是不同于目录2中的文件")
else:
results[file_path] = "这是相同的文件"

好像根本就没有运行到这段代码

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • threenewbee 2017-08-10 15:55
    已采纳
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题