IOError:[Errno 0] Python中的错误

Ada*_*dam 7 python io file-io

我使用Windows 7.

myfile = open("matedata.txt", "a+")
print myfile.readline()
myfile.write("1")
myfile.close()
Run Code Online (Sandbox Code Playgroud)

这不起作用.

myfile.write("1")
IOError: [Errno 0] Error
Run Code Online (Sandbox Code Playgroud)

use*_*680 -2

当你用 python 打开文件时,你需要指定你想要的访问类型(读、写或追加):

  • a - 用于附加
  • r - 用于阅读
  • w - 用于书写

“+”表示如果不存在则更新/创建新的。

你得到 IOError 因为你打开文件并尝试使用相同的 FD(文件解析器)进行读取和写入

  • 不正确。可以使用同一个文件进行读取和写入。请参阅文档(http://docs.python.org/3/library/functions.html#open),其中说“+”并不意味着创建文件,而是意味着打开一个文件以进行读取和写作。 (7认同)