我在尝试打开Python文件时遇到了一个错误。这是我的代码:
>>> import os.path
>>> os.path.isfile('/path/to/file/t1.txt')
>>> True
>>> myfile = open('/path/to/file/t1.txt','w')
>>> myfile
>>> <open file '/path/to/file/t1.txt', mode 'w' at 0xb77a7338>
>>> myfile.readlines()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: File not open for reading
我也尝试过:
for line in myfile:
print(line)
我也遇到了同样的错误。有人知道这个错误为什么会发生吗?
'r'
是默认模式,不必显式给出。 - user395760