472,333 Members | 1,069 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,333 software developers and data experts.

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

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
6 8289
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
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
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
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
Please don't use lowercase "L" as a variable name;

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

Nov 20 '06 #6

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: not 2 swift | last post by:
I thought I would update an old page on which I had used a <INPUT TYPE=image ...> with a <BUTTON><IMG SRC=...></BUTTON> The problem is that...
3
by: TR | last post by:
Is it possible with CSS to prevent this wrapping alignment with a checkbox with a nested label? This is the label of the checkbox that...
3
by: Ben | last post by:
Here's my form: <form name="aForm" method='post'> <input type=file name=file1 onkeypress='KeyPress()'><br> <a id='attachMoreLink'...
4
by: Rick | last post by:
I'm trying to make an input tag visible or hidden based on the value of another input tag, so that when the 1st input tag is changed in anyway, the...
4
by: Steve Swift | last post by:
If it is possible, could someone demonstrate how to make the text entered into an <INPUT TYPE=TEXTfield right-align within the entry field, please?...
0
by: cyberco | last post by:
Opening, reading and writing to a file works fine in mod_python, but using fileinput (with inplace editing) gives me a 'permission denied' with...
2
by: Sreenath Rao Nellutla | last post by:
Hai all, I am trying to create dropdown calendar control with HTML input control by writing JavaScript. But while executing I am getting the error...
2
by: jp2code | last post by:
I have several input fields on my form, and the form works; however, Visual Studio is showing errors, and I would like to get rid of them. When...
1
by: JPhilS | last post by:
Hi to all Webmaster! I have discover this problem when I inserted the scores of the students i a centain subject. I am making a school project...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.