Click to See Complete Forum and Search --> : Seemingly Simple Python Question


Benny B
01-22-2002, 08:00 PM
Hey,

I've looked through the documentation and still not sure how to achieve this, or if it's possible.....basically I want to insert text at the start of a file.

I know I can append at the end of a file by using:

file = open('filename', 'a')

Also I know I could use readlines() then insert the text I need to into the list and then write all the lines back to the file....but that seems messy and I was wondering if there is a way to simply straight insert it into the start of the file, just like its possible to insert at the end of the file.

Thanks...

Strike
01-22-2002, 09:35 PM
That's the only way I know of - reading the file into a temporary storage of some sort (like an array), and then writing the text that precedes it to the file followed by the original text.

kmj
01-22-2002, 10:03 PM
http://www.linuxnewbie.org/cgi-bin/ubbcgi/ultimatebb.cgi?ubb=get_topic&f=14&t=004384


This thread discusses basically the same thing.

jemfinch
01-23-2002, 03:44 AM
Originally posted by Benny B:
Also I know I could use readlines() then insert the text I need to into the list and then write all the lines back to the file....but that seems messy and I was wondering if there is a way to simply straight insert it into the start of the file, just like its possible to insert at the end of the file.


No, it's not possible except by rewriting the whole file.

Jeremy

Benny B
01-24-2002, 06:00 PM
K, thanks guys.
I figured that would be the case, just wanted to make sure I wasn't overlooking something.

Cheers.