newbie Q: delete list few lines in file

Hans Nowak wurmy at earthlink.net
Sun Jun 30 00:07:30 EDT 2002


Peter Hansen wrote:

>>* how to write to file inplace? (or, do i write to a new file, then
>>delete original, and rename the new file?)
> 
> You cannot write files in place.  

<nitpick>

Sure you can:

   f = open("foobar.txt", "w")
   f.write("foo!"*5)
   f.close()

   g = open("foobar.txt", "r+")
   g.seek(0, 0)
   g.write("bar!")
   g.close()

   print open("foobar.txt", "r").read()

prints

   bar!foo!foo!foo!foo!

</nitpick>

-- 
Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA=='))
# decode for email address ;-)
The Pythonic Quarter:: http://www.awaretek.com/nowak/




More information about the Python-list mailing list