|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
def file_display(file_name,line_num):
line_num = line_num.strip()
(begin,end) = line_num.split(":")
if begin == " ":
begin = "1"
if end == " ":
end = "-1"
if begin == "1" and end == "-1":
promt = "全文"
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))
file = open("F:/record.txt")
begin = int(begin) - 1
#begin = int(begin)
end = int(end) #把字符转换成数字类型
lines = end - begin
for i in range(begin):
file.readline() #此时指针指向了第begin行
if lines < 0:
print(file.read()) # read是读取剩余所有字符
else:
for j in range(lines):
print(file.readline(),end = " ") # readline是只读取一行
file.close()
#file_name = "F:/record.txt"
#line_num = ":"
file_name = input(r'请输入要打开的文件(C:\\test.txt):')
line_num = input('请输入需要显示的行数【格式如 13:21 或 :21 或 21: 或 : 】:')
file_display(file_name, line_num)
请输入要打开的文件(C:\\test.txt):F:/record.txt
请输入需要显示的行数【格式如 13:21 或 :21 或 21: 或 : 】::
文件F:/record.txt从第行到第行的内容如下:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-19-54e6c973cee7> in <module>
44 file_name = input(r'请输入要打开的文件(C:\\test.txt):')
45 line_num = input('请输入需要显示的行数【格式如 13:21 或 :21 或 21: 或 : 】:')
---> 46 file_display(file_name, line_num)
<ipython-input-19-54e6c973cee7> in file_display(file_name, line_num)
23
24 file = open("F:/record.txt")
---> 25 begin = int(begin) - 1
26 #begin = int(begin)
27 end = int(end) #把字符转换成数字类型
ValueError: invalid literal for int() with base 10: ''
错误原因是什么?如何修改?
|
|