【发布时间】:2015-03-05 16:32:42
【问题描述】:
我正在尝试显示文件中的文本,但收到了上述错误。我尝试使用os.chdir() 更改默认目录。即使我将文本文件保存在默认目录中,我也会收到错误消息。我使用的是 3.2 版。
代码:
from sys import argv
import os
script, filename = argv
txt = open(filename)
print(filename, "is the filename.")
print("Here is the text.", txt.read())
filename_again = input("Filename again pls.")
txt_again = open(filename_again)
print("Test again.", txt_again.read())
错误报告:
PS C:\Python32\Stuff\Programs> python Sample1.py fubar1.txt
Traceback (most recent call last):
File "Sample1.py", line 6, in <module>
txt = open(filename)
IOError: [Errno 2] No such file or directory: 'fubar1.txt'
PS C:\Python32\Stuff\Programs>
【问题讨论】:
-
给定文件名中可能有错误。
-
在第二次打开文件之前输入
txt.close()还会报错吗? -
那些双斜线让我很担心。转义字符有什么奇怪的地方吗?
-
@AvinashRaj- 我考虑过。尝试更改文件名,但仍然收到错误。
-
@Kevin- 是的,仍然出现错误。
标签: python io python-3.2