edit text in a file and save it again

Fredrik Lundh fredrik at pythonware.com
Mon Nov 20 01:45:42 EST 2006


cyberco wrote:

> I must be overlooking something here... I'm trying to edit a line in a
> text file. I thought this was easy with fileinput, but all examples do
> not write the line back to the file but simply 'print' it. I want to
> open the file, edit the line and save it again. Is fileinput the right
> module for that?

   http://docs.python.org/lib/module-fileinput.html

   Optional in-place filtering: if the keyword argument inplace=1 is
   passed to input() or to the FileInput constructor, the file is moved
   to a backup file and standard output is directed to the input file (if
   a file of the same name as the backup file already exists, it will be
   replaced silently). This makes it possible to write a filter that
   rewrites its input file in place.

</F>




More information about the Python-list mailing list