|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- def file_view(file_name, line_num):
- # if line_num.strip() == ':':
- # begin = '1'
- # end = '-1'
-
- (begin, end) = line_num.split(':')
- if begin == '':
- begin = '1'
- if end == '':
- end = '-1'
- if begin == '1' and end == '-1':
- prompt = '的全文'
- elif begin == '1':
- prompt = '从开始到%s' % end
- elif end == '-1':
- prompt = '从%s到结束' % begin
- else:
- prompt = '从第%s行到第%s行' % (begin, end)
- print('\n文件%s%s的内容如下:\n' % (file_name, prompt))
- begin = int(begin) - 1
- end = int(end)
- lines = end - begin
- f = open(file_name)
-
- for i in range(begin): # 用于消耗掉begin之前的内容
- f.readline()
- if lines < 0:
- print(f.read())
- else:
- for j in range(lines):
- print(f.readline(), end='')
-
- f.close()
- file_name = input(r'请输入要打开的文件(C:\\test.txt):')
- line_num = input('请输入需要显示的行数【格式如 13:21 或 :21 或 21: 或 : 】:')
- file_view(file_name, line_num)
复制代码
if line_num.strip() == ':':
begin = '1'
end = '-1' 这句被我注释掉了 运行都没有报错
本帖最后由 °蓝鲤歌蓝 于 2018-3-8 20:23 编辑
你在你的第二个输入的数据前面或后面加上个':'试试就知道了。
只是为了删除前面和后面的':'而已,你输入正确的话注释掉也不会报错。
还有贴代码要说一下你的程序是做什么的,而且最好是有注释,不然有些大佬不会浪费时间看的。
|
|