python 在python中seek(),然后read(),然后write()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/783792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:51:33  来源:igfitidea点击:

seek(), then read(), then write() in python

pythonfile-io

提问by daphshez

When running the following python code:

运行以下python代码时:

>>> f = open(r"myfile.txt", "a+")        
>>> f.seek(-1,2)                                        
>>> f.read()                                            
'a'                                                     
>>> f.write('\n')                                        

I get the following (helpful) exception:

我收到以下(有用的)异常:

Traceback (most recent call last):      
  File "<stdin>", line 1, in <module>   
IOError: [Errno 0] Error        

The same thing happens when openning with "r+".

用“r+”打开时也会发生同样的事情。

Is this supposed to fail? Why?

这应该失败吗?为什么?

Edit:

编辑:

  1. Obviously, this is just an example, not what I am actually trying to execute. My actual goal was to verify that the files ends with "\n", or add one, before adding the new lines.
  2. I am working under Windows XP, and they problem exists in both Python 2.5 and Python 2.6.
  3. I managed to bypass the problem by calling seek() again:

    f = open(r"myfile.txt", "a+")
    f.seek(-1,2)
    f.read()
    'a'
    f.seek(-10,2)
    f.write('\n')

  1. 显然,这只是一个例子,并不是我真正想要执行的。我的实际目标是在添加新行之前验证文件是否以“\n”结尾或添加一个。
  2. 我在 Windows XP 下工作,它们的问题存在于 Python 2.5 和 Python 2.6 中。
  3. 我通过再次调用 seek() 设法绕过了这个问题:

    f = open(r"myfile.txt", "a+")
    f.seek(-1,2)
    f.read()
    'a'
    f.seek(-10,2)
    f.write('\n')

The actual parameters of the 2nd seek call don't seem to matter.

第二次搜索调用的实际参数似乎无关紧要。

采纳答案by Lance Richardson

This appears to be a Windows-specific problem - see http://bugs.python.org/issue1521491for a similar issue.

这似乎是 Windows 特定的问题 -有关类似问题,请参阅http://bugs.python.org/issue1521491

Even better, a workaround given and explained at http://mail.python.org/pipermail/python-bugs-list/2005-August/029886.html, insert:

更好的是,在http://mail.python.org/pipermail/python-bugs-list/2005-August/029886.html给出并解释了一种解决方法,插入:

f.seek(f.tell())

between the read() and write() calls.

在 read() 和 write() 调用之间。

回答by John T

the a+ mode is for appending, if you want to read & write, you are looking for r+.

a+ 模式用于追加,如果你想读写,你正在寻找 r+。

try this:

试试这个:

>>> f = open("myfile.txt", "r+")
>>> f.write('\n')

Edit:

编辑:

you should have specified your platform initially... there are known problems with seek within windows. When trying to seek, UNIX and Win32 have different line endings, LF and CRLF respectively. There is also an issue with reading to the end of a file. I think you are looking for the seek(2) offset for the end of the file, then carry on from there.

您应该一开始就指定了您的平台……在 Windows 中查找存在已知问题。尝试查找时,UNIX 和 Win32 具有不同的行尾,分别是 LF 和 CRLF。读取到文件末尾也存在问题。我认为您正在寻找文件末尾的 seek(2) 偏移量,然后从那里继续。

these articles may be of interest to you (the second one more specifically):

您可能对这些文章感兴趣(更具体地说是第二篇):

http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-08/2512.html

http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-08/2512.html

http://mail.python.org/pipermail/python-list/2002-June/150556.html

http://mail.python.org/pipermail/python-list/2002-June/150556.html

回答by nosklo

Works for me:

对我有用:

$ echo hello > myfile.txt
$ python
Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open('myfile.txt', 'r+')
>>> f.seek(-1, 2)
>>> f.tell()
5L
>>> f.read()
'\n'
>>> f.write('\n')
>>> f.close()

Are you on windows? If so, try 'rb+'instead of 'r+'in the mode.

你在窗户上吗?如果是这样,请尝试'rb+'而不是'r+'在模式中。