How do I read and write to the same CSV file

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Mon Sep 10 10:32:40 EDT 2007


On Mon, 10 Sep 2007 07:27:46 -0700, Chris wrote:

> if date == 0:
>       date = os.getmtime()
>       # now this is where I want to write into the row of the same csv
> file but only updating that one field
> 
> How can I do this?

You can't.  Modifying text files with variable length lines is
"impossible" or at least not worth the trouble because if the replacement
line does not have the very same length, all lines after that changed line
have to be moved, i.e. rewritten anyway.

Write the rows to another file and modify the desired entry on the fly and
rename the temporary file to the source file's name afterward.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list