【发布时间】:2012-06-24 10:34:27
【问题描述】:
我得到了一个错误“IOError: [Errno 0] Error”这个python程序:
from sys import argv
file = open("test.txt", "a+")
print file.tell() # not at the EOF place, why?
print file.read() # 1
file.write("Some stuff will be written to this file.") # 2
# there r some errs when both 1 & 2
print file.tell()
file.close()
似乎是什么问题?以下这两种情况都可以:
from sys import argv
file = open("test.txt", "a+")
print file.tell() # not at the EOF place, why?
# print file.read() # 1
file.write("Some stuff will be written to this file.") # 2
# there r some errs when both 1 & 2
print file.tell()
file.close()
和:
from sys import argv
file = open("test.txt", "a+")
print file.tell() # not at the EOF place, why?
print file.read() # 1
# file.write("Some stuff will be written to this file.") # 2
# there r some errs when both 1 & 2
print file.tell()
file.close()
还是,为什么
print file.tell() # not at the EOF place, why?
不打印文件的大小,“a+”是附加模式吗?那么文件指针应该指向EOF?
我使用的是 Windows 7 和 Python 2.7。
【问题讨论】:
-
你从哪里得到错误?问题似乎是您正在尝试读取以附加模式打开的文件
-
另外,你确定 text.txt 存在吗?
-
您的代码对我来说很好用。
tell在打开文件后就返回0,当然,你为什么会期待别的呢? -
可以添加你使用的Python版本和操作系统吗?
-
查看我更新的答案,这个修复应该可以工作。我在和你一样的平台+python版本上试过了