fileinput.input('test.txt') => ERROR: input() already active

Fredrik Lundh fredrik at pythonware.com
Mon Nov 20 11:12:24 CET 2006


cyberco wrote:

> Another related question I have: The following piece edits in place,
> but ads whitelines between all lines of a Windows text file. Why?
> 
> ===========================
> fi = fileinput.input('test.txt', inplace=1)
> for l in fi:
>     print l.replace('a', 'b')
> ===========================

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

     /.../ lines are returned including the trailing newline when it
     is present.

and "print" adds its own newline, of course.  try

     print text,

or

     sys.stdout.write(text)

</F>




More information about the Python-list mailing list