Python文件操作 [英] Python file operations

查看:46
本文介绍了Python文件操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此python程序出现错误"IOError:[Errno 0] Error":

I got an err "IOError: [Errno 0] Error" with this python program:

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()

似乎是什么问题?以下这两种情况都可以:

what seems to be the problem? These 2 cases below are ok:

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?

does not print the size of the file, is "a+" the append-mode? then the file pointer should point to EOF?

我正在使用Windows 7和Python 2.7.

i'm using Windows 7 and Python 2.7.

推荐答案

Python使用stdio的fopen函数并将模式作为参数传递.我假设您使用Windows,因为@Lev说代码在Linux上工作正常.

Python uses stdio's fopen function and passes the mode as argument. I am assuming you use windows, since @Lev says the code works fine on Linux.

以下内容来自Windows的打开文档,可能是解决问题的线索:

The following is from the fopen documentation of windows, this may be a clue to solving your problem:

指定"r +","w +"或"a +"访问类型时,两个 并允许写入(据说该文件已打开以进行更新"). 但是,当您在阅读和写作之间切换时,必须有一个 介入fflush,fsetpos,fseek或倒带操作.目前 可以为fsetpos或fseek操作指定位置,如果

When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for "update"). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired.

因此,解决方案是在file.write()调用之前添加file.seek().要附加到文件末尾,请使用file.seek(0, 2).

So, the solution is to add file.seek() before the file.write() call. For appending to the end of the file, use file.seek(0, 2).

file.seek的工作原理如下:

For your reference, file.seek works as follows:

要更改文件对象的位置,请使用f.seek(offset,from_what). 通过将偏移量添加到参考点来计算位置;这 参考点由from_what参数选择.一个from_what 值0从文件的开头开始测量,1使用当前值 文件位置,而2使用文件的末尾作为参考点. from_what可以省略,默认为0,使用 文件作为参考点.

To change the file object’s position, use f.seek(offset, from_what). The position is computed from adding offset to a reference point; the reference point is selected by the from_what argument. A from_what value of 0 measures from the beginning of the file, 1 uses the current file position, and 2 uses the end of the file as the reference point. from_what can be omitted and defaults to 0, using the beginning of the file as the reference point.

[参考: http://docs.python.org/tutorial/inputoutput.html]

如@lvc在评论中和@Burkhan在其回答中所提到的,您可以使用

As mentioned by @lvc in the comments and @Burkhan in his answer, you can use the newer open function from the io module. However, I want to point out that the write function does not work exactly the same in this case -- you need to provide unicode strings as input [Simply prefix a u to the string in your case]:

from io import open
fil = open('text.txt', 'a+')
fil.write('abc') # This fails
fil.write(u'abc') # This works

最后,请避免使用名称文件"作为变量名,因为它是指内置类型,并且将被静默覆盖,从而导致一些难以发现的错误.

Finally, please avoid using the name 'file' as a variable name, since it refers to a builtin type and will be silently over-written, leading to some hard to spot errors.

这篇关于Python文件操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆