appending a file

Jeff Collins collins at seal.aero.org
Tue Mar 14 13:13:59 EST 2000


This is most likely a better way than this, but the solution
popped into my head upon reading your question.  If you are
using Unix, try:

fh = open('jnkxxx','w')
fh.write('line 1\n')
fh.write('line 2\n')
fh.close()

# cat jnkxxx
# line 1
# line 2

import os

fh = os.popen("cat >> jnkxxx", "w")
fh.write('line 3\n')
fh.write('line 4\n')
fh.close()

# cat jnkxxx
# line 1
# line 2
# line 3
# line 4


Jeff


Lenny Self writes:
 > Hello.
 > 
 > I am a little frustrated. After looking all over the place ( Learning
 > Python, the Python Pocket Reference, Library Reference) I have been unable
 > to find how to append to the end of an existing text file using python in
 > any way other than reading the entire contents of the file out to a list,
 > adding the additional information, and then writing it all back to the
 > original file.  I know there must  be a better way to do this; for the life
 > of me, however, I seem to me unable to find it.
 > 
 > Can anyone help me out?
 > 
 >     -- Lenny Self
 >         lenny at squiggie.com
 > 
 > 
 > -- 
 > http://www.python.org/mailman/listinfo/python-list
 > 




More information about the Python-list mailing list