How to merge data in a existant file

Andrew M. Kuchling akuchlin at cnri.reston.va.us
Mon Apr 19 09:03:35 EDT 1999


fquiquet at 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





More information about the Python-list mailing list