Problem with subprocess module on Windows with open file in append mode

MRAB python at
Tue Oct 6 16:24:23 CEST 2009

Gabriel Genellina wrote:
> En Sat, 03 Oct 2009 21:53:12 -0300, Andrew Savige 
> <ajsavige at> escribió:
>> When I run this little test program on Linux:
>> import subprocess
>>["python","-V"], stderr=open("log.tmp","a"))
>> the file log.tmp is appended to each time I run it.
>> When I run it on Windows, however, the file log.tmp gets
>> overwritten each time I run it.
>> Though I can make it append on Windows like this:
>> import os
>> import subprocess
>> f = open("log.tmp", "a")
>>, os.SEEK_END)
>>["python","-V"], stderr=f)
>> I don't understand why that should be necessary.
>> Is this a Python/subprocess bug on Windows?
> No, it's an "implementation-defined behavior" of the underlying C 
> library. From the C89 standard:
> """4.9.3 Files
> [...] If a
> file can support positioning requests (such as a disk file, as opposed
> to a terminal), then a file position indicator associated with
> the stream is positioned at the start (character number zero) of the
> file, unless the file is opened with append mode in which case it is
> implementation-defined whether the file position indicator is
> positioned at the beginning or the end of the file."""
> The Linux implementation choose to move the file pointer to the end in 
> such cases, the Windows one choose to always keep it at the start. Both 
> are correct according to the specification above. Python 2.X just 
> inherits that behavior from C. If you want to write portable code, 
> you'll have to use the longer form.
That's strange: whenever I've opened a file for append in Microsoft
Visual C/C++ (or Python or Delphi, for that matter) on Windows I've
assumed that the file pointer would be at the end of file, and that's
always proved to be the case!

More information about the Python-list mailing list