python3遇到了OSError: [Errno 22] Invalid argument

`
请问这个是怎么回事,需要怎么解决呢?在笨方法学python中看到的例子写过来就出现了这个错误!

from sys import argv
script,filename=argv
txt =open(filename)
print('here is your file %r:'%filename)
print(txt.read())
txt.close()
print('Type the filename again:')
file_again=input(">")
txt_again=open(r'"E:\source code\python\ex15_sample.txt"')
print(txt_again.read())
txt_again.close()

运行时

C:\Users\winneter>python "E:\source code\python\ex15.py" "E:\source code\python\ex15_sample.txt"
here is your file 'E:\\source code\\python\\ex15_sample.txt':
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
Type the filename again:
>ex15_sample.txt
Traceback (most recent call last):
  File "E:\source code\python\ex15.py", line 9, in <module>
    txt_again=open(r'"E:\source code\python\ex15_sample.txt"')
OSError: [Errno 22] Invalid argument: '"E:\\source code\\python\\ex15_sample.txt"'
阅读 25.9k
2 个回答

下面这句写错了:
txt_again=open(r'"E:\ource code\python\ex15_sample.txt"')
应该是:
txt_again=open(r'E:\source code\python\ex15_sample.txt')

下面这句写错了:
txt_again=open(r'"E:\source code\python\ex15_sample.txt"')
应该是:
txt_again=open(r'E:\\source code\python\ex15_sample.txt')
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题