# -*- coding:gbk -*-
def file_view(file_name , line_num):
line_num = line_num . strip()
if ':' in line_num :
begin , end = line_num . split(':')
if not len(begin) :
begin = '1'
if not len(end):
end = '-1'
begin , end = int(begin) , int(end)
if (begin > 0 and end >= begin) or end == - 1:
if begin == end:
prompt = '第 %d 行' % begin
else:
if begin == 1:
prompt = '从开始'
else:
prompt = '从第 %d 行' % begin
if end == -1:
prompt += '到结束'
else:
prompt += '到第 %d 行' % end
print(prompt)
f = open(file_name)
c = 0
for each in f:
c += 1
if c >= begin and (c <= end or end == -1) :
print(each[:-1])
elif end != -1 and c > end:
break
f . close()
else:
print('输入参数数值无效!')
else:
print('输入参数格式错误!')
file_name=input('请输入你想要打开的文件(C:\\test.txt): ')
line_num=input('请输入需要显示的行数【格式如 13:21 或 :21 或 21: 或:】:')
file_view(file_name,line_num)
|