Changing a line in a text file
Steve Holden
steve at holdenweb.com
Mon Apr 25 06:23:29 EDT 2005
kah wrote:
> Hi,
>
> the example provided by Vishnu is quite close to what I want but it
> still required me to write all the data back to the file.
>
> Is there any way where I can just write a particular line?
>
If you are asking whether you can update a file in place, the answer is
"yes" - look up "lseek" in the Python documentation (module os, under
"File Descriptor Operations".
However, you asked about replacing one line with another of a different
length: since this will mean changing the offsets of all subsequent
bytes you have no way to do this other than writing out the whole
content of the file following the modification. You would also have to
ensure that you truncated the file to the correct length.
In general, although they don't make it obvious that they are doing so
most programs that "change" files (text editors and the like) are really
writing new copies.
regards
Steve
--
Steve Holden +1 703 861 4237 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
More information about the Python-list
mailing list