|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
firstfile = input('请输入需要比较的头一个文件名:')
secondfile = input('请输入需要比较的另一个文件名:')
f = open('D:\\桌面\\'+ firstfile)
g = open('D:\\桌面\\'+ secondfile)
different = []
count = 1
for line0 in f:
line1 = g.readline()
if line0 != line1:
different.append(count)
count += 1
f.close()
g.close()
print('两个文件共有【%d】处不同:'%len(different))
for i in different:
print('第%d行不一样'%different[i])
报错:
请输入需要比较的头一个文件名:record0.txt
请输入需要比较的另一个文件名:record1.txt
回溯(最后调用):
File "D:\桌面\py练习代码\作业1.py", line 7, in <module>
for line0 in f:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 4: illegal multibyte sequence
在 open 里面加上 encoding ='utf-8'参数试试,更改下编码:
- f = open('D:\\桌面\\'+ firstfile,encoding='utf-8')
- g = open('D:\\桌面\\'+ secondfile,encoding='utf-8')
复制代码
|
|