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

C

cyberco

Using fileinput.input('test.txt') I probably forgot to process all
lines or so, since I get the error 'input() already active' when i try
to call fileinput.input('test.txt') again. But how can I 'close' the
previous version I opened? I have no handle to it or so...
 
P

Peter Otten

cyberco said:
Using fileinput.input('test.txt') I probably forgot to process all
lines or so, since I get the error 'input() already active' when i try
to call fileinput.input('test.txt') again. But how can I 'close' the
previous version I opened? I have no handle to it or so...

fileinput.close()

Or you forego the global FileInput instance with

fi = fileinput.FileInput(...)
for line in fi:
# process line
fi.close()

Personally, I use ordinary files instead.

Peter
 
C

cyberco

Ah, thanks!
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')
===========================
 
F

Fredrik Lundh

cyberco said:
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>
 
J

John Machin

cyberco said:
Ah, thanks!
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:

Please don't use lowercase "L" as a variable name; it is too easily
confused with the digit 1 in some fonts. Use meaningful names.
print l.replace('a', 'b')
===========================
fi = fileinput.input('test.txt', inplace=1)
for line in fi:
print line.replace('a', 'b')

It's nothing to do with whether the file is a "Windows text file" or
not. The reason is that line already has a "\n" i.e. newline character
at the end. The print statement adds another.
Either:
(1) change
print blahblah
to
print blahblah, # comma suppresses the added newline
or
(2) put
import sys
up the front and change
print blahblah
to
sys.stdout.write(blahblah)

HTH,
John
 
C

cyberco

Please don't use lowercase "L" as a variable name;

Ohoh...I plead guilty! I totally forgot about CS 101... :)
 
J

John Machin

cyberco said:
Ohoh...I plead guilty! I totally forgot about CS 101... :)

I don't understand the reference to CS 101.

It's quite simple: If you want people to bother reading your postings
and help you, make them easy to comprehend. Otherwise they won't bother
a second time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top