Bug on Python2.3.4 [FreeBSD]?

David Bolen db3l at fitlinxx.com
Fri Aug 12 16:12:15 EDT 2005


Uwe Mayer <merkosh at hadiko.de> writes:

> AFAICT there seems to be a bug on FreeBSD's Python 2.3.4 open function. The
> documentation states:
> 
> > Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+'
> > truncates the file). Append 'b' to the mode to open the file in binary
> > mode, on systems that differentiate between binary and text files (else it
> > is ignored). If the file cannot be opened, IOError is raised.   
> 
> Consider:
> 
> $ cat test
> lalala
> 
> $ python2.3
> Python 2.3.4 (#2, Jan  4 2005, 04:42:43)
> [GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
> Type "help", "copyright", "credits" or "license" for more information.
> >>> f = open('test', 'r+')
> >>> f.read()
> 'lalala\n'
> >>> f.write('testing')
> >>> f.close()
> >>>
> [1]+  Stopped                 python2.3
> $ cat test
> lalala
> 
> -> write did not work; ok

Strange, I tried this with Python 2.3.3 and 2.3.5 on two FreeBSD 4.10
systems and it seemed to append to the file properly in both cases.
Going back further, it also worked with Python 2.2.2 on a FreeBSD 4.7
system.  I don't see happen to have a 2.3.4 installation, but can't
see any changes to the source for the file object between 2.3.4 and
2.3.5, for example.

~> python
Python 2.3.5 (#2, May  5 2005, 11:11:17)
[GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open('test','r+')
>>> f.read()
'lalala\n'
>>> f.write('testing')
>>> f.close()
>>>
~> cat test
lalala
testing  # no newline was present

Which version of FreeBSD are you running?  I thought it might be a
dependency on needing to seek between reads and writes on a duplex
stream (which is ANSI), but FreeBSD doesn't require that, at least
back as far as a 4.7 system I have, and I assume much earlier than
that.

One dumb question - are you absolutely sure it wasn't appending?  As
written, there's no trailing newline on the file, so your final "cat
test" would produce output where the "testing" was on the same line as
your next command prompt, and can sometimes be missed visually.

> Can anyone confirm that? Is there any other way of opening a file for
> appending instead of a+? 

Well, if you really just want appending, I'd just use "a".  It creates
the file if necessary but always appends to the end.  Of course, it's
not set up for reading, but you wouldn't need that for appending.

-- David



More information about the Python-list mailing list