Mailing List Archive

How to merge data in a existant file
Hi,

I use python to developp script file.
I have a problem when I want to write data in a existant file.

I know how to write data in a new file :

f=open('file.name','w')
f.write('data')
f.close()

I don't know what is the function that permit to add data whithout erase
existing data.

Thank's for your response to fquiquet@lemel.fr

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
How to merge data in a existant file [ In reply to ]
Hi!

On Mon, 19 Apr 1999 fquiquet@lemel.fr wrote:
> I know how to write data in a new file :
>
> f=open('file.name','w')
> f.write('data')
> f.close()
>
> I don't know what is the function that permit to add data whithout erase
> existing data.

f=open('file.name', 'a')
^ append
Other file modes are 'r+' and 'w+'. Read python documenataion on file
modes and open(). Remember to use 'b' when working with binary files
(mode = 'rb', for example.)

> Thank's for your response to fquiquet@lemel.fr

Oleg.
----
Oleg Broytmann National Research Surgery Centre http://sun.med.ru/~phd/
Programmers don't die, they just GOSUB without RETURN.
How to merge data in a existant file [ In reply to ]
fquiquet@lemel.fr writes:
>I know how to write data in a new file :
>I don't know what is the function that permit to add data whithout erase
>existing data.

Open the file in append mode, with a mode string of 'a' (or
'ab' for a binary file) instead of 'w'.

>>> f = open('test-file', 'w') ; f.write('abc\n') ; f.close()
>>> f = open('test-file', 'a') ; f.write('abc\n') ; f.close()
>>> open('test-file', 'r').read()
'abc\012abc\012'

--
A.M. Kuchling http://starship.python.net/crew/amk/
The NSA regularly lies to people who ask it for advice on export control. They
have no reason not to; accomplishing their goal by any legal means is fine by
them. Lying by government employees is legal.
-- John Gilmore