appending to top of file
Greg Jorgensen
gregj at pobox.com
Sun Dec 31 23:28:06 EST 2000
"ktb" <x.y.f at home.com> wrote in message
news:mailman.978315002.31351.python-list at python.org...
> Hi, I'm new to python. Just installed it and read through some
> of the docs. My question is, can I append lines to the
> beginning of a file as opposed to the end? I have a web log
> that I would like to write to the file I pull in with php. I'm
> running Linux and python version 1.5.2. I searched though some
> of the back archives but didn't have any luck.
You can only append to the end of a file. You'll have to make a new file,
writing the new content first, then copying the old content.
A more scalable way to handle applications like you describe is to put each
chunk of content (article, log entry, etc.) into a separate file, or into a
database (like mySQL), and construct the HTML page dynamically. For example
if you had a directory of log entries named by date (log-20001229.txt,
log-20001231.txt, log-20010101.txt, etc.) you could easily get a list of
filenames, sort by date, and output each entry. A database engine will be
faster and easier to manage once you get a moderate number of entries.
--
Greg Jorgensen
PDXperts
Portland, Oregon, USA
gregj at pobox.com
More information about the Python-list
mailing list