|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
第一题,按小甲鱼老师的代码,执行后报错
用的是python2.7
- # -*- coding:cp936 -*-
- #编写一个程序,比较用户输入的两个文件,如果不同,显示出所有不同处的行号与第一个不同字符的位置
- def file_compare(file1,file2):
- f1 =((open(file1)))
- f2 =((open(file2)))
- count = 0 #统计行数
- differ = [] #统计不一样的数量
- for line1 in f1:
- line2 = str(f2.readline())
- count += 1
- if line1 != line2:
- differ.append(count)
- f1.close()
- f2.close()
- return differ
- file1 = input('请输入需要比较的第一个文件名:')
- file2 = input('请输入需要比较的第二个文件名:')
- differ =(file_compare(file1,file2))
- if len(differ) == 0:
- print('两个文件完全一样!')
- else:
- print('两个文件共有【%d】处不同:' % len(differ))
- for each in differ:
- print('第%d 行不一样' % each)
复制代码
报错信息:
- >>> ================================ RESTART ================================
- >>>
- 请输入需要比较的第一个文件名:123
- 请输入需要比较的第二个文件名:321
- Traceback (most recent call last):
- File "I:\python exercise\029lesson\t1.py", line 25, in <module>
- differ =(file_compare(file1,file2))
- File "I:\python exercise\029lesson\t1.py", line 7, in file_compare
- f1 =((open(file1)))
- TypeError: coercing to Unicode: need string or buffer, int found
复制代码 |
|