| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
代码如下: 
def file_compare(file1, file2): 
    f1 = open(file1,encoding='UTF-8') 
    f2 = open(file2,encoding='UTF-8') 
    count = 0 # 统计行数 
    differ = [] # 统计不一样的数量 
    for line1 in f1: 
        print(line1) 
        line2 = f2.readline() 
        print(line2) 
        count += 1 
        if line1 != line2: 
            differ.append(count) 
    f1.close() 
    f2.close() 
    return differ 
 
file1 = input('请输入需要比较的头一个文件名:') 
file2 = input('请输入需要比较的另一个文件名:') 
differ = file_compare(file1, file2) 
print(differ) 
if len(differ) == 0: 
    print('两个文件完全一样!') 
else: 
    print('两个文件共有【%d】处不同:' % len(differ)) 
    for each in differ: 
        print('第 %d 行不一样' % each) 
 
显示编码不对以后采用了encoding='UTF-8',奈何显示的错误又变了:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb4 in position 0: invalid start byte
 |   
 
 
 
 |