read/write file problem

Thomas Wouters thomas at xs4all.net
Mon Apr 17 10:51:02 EDT 2000


On Mon, Apr 17, 2000 at 07:36:53AM -0600, j vickroy wrote:

> I am attempting to read and, subsequently, write a jpeg file.
> The following interactive session illustrates the problem I am having
> (i.e., if the source file is read byte-by-byte into memory and
> subsequently
> rewritten using the file.write() method, only the initial portion of the
> file appears to be written).
> How should I be doing this in Python 1.5.2.


> >>> file = open (file_name, 'w')
> >>> for i in xrange (0,source_byte_cnt):
> ...  file .write (buf[i])

[...]

> >>> file = open (file_name, 'w')

This overwrites the previously written portion, and since you write the same
amount of bytes, it seems unchanged. Change this to:

file = open (file_name, 'a')

and it will work as expected ('a' is append, which means writing to the file
from the end onward.)

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list