7

When running the following python code:

>>> 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+".

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

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

daphshez
  • 9,272
  • 11
  • 47
  • 65
  • What operating system and python version are you using? This appears to work fine with Python 2.4.3 under Linux 2.6.17. – Lance Richardson Apr 23 '09 at 22:35
  • Is that exactly what you want to be doing, or is it a simplification? If that's the task at hand, why not just open as 'a' and then call f.write, with no intervening seek? – kyle Apr 23 '09 at 22:36
  • Also (to Lance), it's not working at all in Python 2.5 on Windows XP. – kyle Apr 23 '09 at 22:37
  • OK, I see the same error with Python 2.4.3 under Windows XP Pro. Hmmm... – Lance Richardson Apr 23 '09 at 22:37

3 Answers3

5

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

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

f.seek(f.tell())

between the read() and write() calls.

Lance Richardson
  • 4,610
  • 23
  • 30
  • 3
    Or, rather, f.seek(0, 1), to avoid an extra function call. – tzot Apr 25 '09 at 14:16
  • I encounter a similar problem that is solved by this trick, but I am using Xubuntu 15.04 with Python 3.4.3. See [my question](https://stackoverflow.com/questions/40552106/file-object-methods-write-or-writelines-start-writing-at-unexpected-position) –  Nov 11 '16 at 17:50
1

the a+ mode is for appending, if you want to read & write, you are looking for 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.

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://mail.python.org/pipermail/python-list/2002-June/150556.html

John T
  • 23,735
  • 11
  • 56
  • 82
0

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.

nosklo
  • 217,122
  • 57
  • 293
  • 297