|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
'''29课第2题. 编写一个程序,当用户输入文件名和行数(N)后,
将该文件的前N行内容打印到屏幕上。'''
def xianshi(filename,hangshu):
with open(filename,encoding='utf-8') as file:
count = 0
print('\n文件%s的前%s的内容如下:\n' % (filename,hangshu))
while True:
line = file.readline()
count+=1
if count >int(hangshu)-1:
break
else:
print(line)
file.close()
filename = input('请输入需要打开的文件(c:\\test.txt):')
hangshu = input('请输入需要显示的该文件前几行:')
xianshi(filename,hangshu)
#下面是报错的原因,昨天还正常执行,今天程序就报错,莫名其妙,程序没有任何改动
======================== RESTART: D:\pythonlx\29..小甲鱼.py =======================
请输入要打开的文件(C:\\test.txt):)D:\test.txt
请输入需要显示该文件前几行:8
文件)D:\test.txt的前8的内容如下:
Traceback (most recent call last):
File "D:\pythonlx\29..小甲鱼.py", line 11, in <module>
file_view(file_name, line_num)
File "D:\pythonlx\29..小甲鱼.py", line 3, in file_view
with open(file_name,encoding='utf-8') as f:
OSError: [Errno 22] Invalid argument: ')D:\\test.txt'
>>>
请输入要打开的文件(C:\\test.txt):)D:\test.txt
你多输入了一个括号
|
|