By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,374 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

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

P: n/a
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...

Nov 20 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
cyberco wrote:
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
Nov 20 '06 #2

P: n/a
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')
===========================

Nov 20 '06 #3

P: n/a
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>

Nov 20 '06 #4

P: n/a
cyberco wrote:
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

Nov 20 '06 #5

P: n/a
Please don't use lowercase "L" as a variable name;

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

Nov 20 '06 #6

P: n/a

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

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.

Nov 20 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.