473,240 Members | 1,638 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Does fileinput.input() read STDIN all at once?

I'm using this sort of standard thing:

for line in fileinput.input():
do_stuff(line)

and wondering whether it reads until it hits an EOF and then passes
lines (one at a time) into the variable line. This appears to be the
behaviour when it's reading STDIN interactively (i.e. from the
keyboard).

As a test, I tried this:

for line in fileinput.input():
print '**', line

and found that it would print nothing until I hit Ctl-D, then print
all the lines, then wait for another Ctl-D, and so on (until I pressed
Ctl-D twice in succession to end the loop).

Is it possible to configure this to pass each line of input into line
as it comes?

Thanks,
Adam
Dec 18 '07 #1
4 3203
On Dec 18, 5:55 am, Adam Funk <a24...@ducksburg.comwrote:
I'm using this sort of standard thing:

for line in fileinput.input():
do_stuff(line)

and wondering whether it reads until it hits an EOF and then passes
lines (one at a time) into the variable line. This appears to be the
behaviour when it's reading STDIN interactively (i.e. from the
keyboard).

As a test, I tried this:

for line in fileinput.input():
print '**', line

and found that it would print nothing until I hit Ctl-D, then print
all the lines, then wait for another Ctl-D, and so on (until I pressed
Ctl-D twice in succession to end the loop).
There is probably a 1024 byte buffer. Try filling it up and see if you
get something before you hit CTRL-D.

Otherwise, the writers have to flush the buffer if they want a line to
be sent before the buffer fills up. C++ endl would do this, I believe,
in addition to printing '\n'.

Note that terminals (what you get if you are typing from the command
line) are terminals and behave quite differently than open files.
Is it possible to configure this to pass each line of input into line
as it comes?
It's up to the writer to flush the buffers. There's not much you can
do, so just learn to live with it.

It sounds like you want to write some kind of interactive program for
the terminal. Do yourself a favor and use curses or go with a full-
blown GUI.
Dec 18 '07 #2
On 2007-12-18, Jonathan Gardner wrote:
>As a test, I tried this:

for line in fileinput.input():
print '**', line

and found that it would print nothing until I hit Ctl-D, then print
all the lines, then wait for another Ctl-D, and so on (until I pressed
Ctl-D twice in succession to end the loop).
There is probably a 1024 byte buffer. Try filling it up and see if you
get something before you hit CTRL-D.
Thanks; I'm looking into the buffering question.

It sounds like you want to write some kind of interactive program for
the terminal. Do yourself a favor and use curses or go with a full-
blown GUI.
No, I'm really interested in reading the input from files!

This just came up because I was trying to test the program by giving
it no filename arguments and typing sample input.
Dec 19 '07 #3
Adam Funk <a2****@ducksburg.comwrote:
>
I'm using this sort of standard thing:

for line in fileinput.input():
do_stuff(line)

and wondering whether it reads until it hits an EOF and then passes
lines (one at a time) into the variable line. This appears to be the
behaviour when it's reading STDIN interactively (i.e. from the
keyboard).

As a test, I tried this:

for line in fileinput.input():
print '**', line

and found that it would print nothing until I hit Ctl-D, then print
all the lines, then wait for another Ctl-D, and so on (until I pressed
Ctl-D twice in succession to end the loop).
Note that you are doing a lot more than just reading from a file here. This
is calling a function in a module. Fortunately, you have the source to
look at.

As I read the fileinput.py module, this will eventually call
FileInput.next(), which eventually calls FileInput.readline(), which
eventually calls stdin.readlines(_bufsize). The default buffer size is
8,192 bytes.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Dec 20 '07 #4
On 2007-12-20, Tim Roberts wrote:
>>As a test, I tried this:

for line in fileinput.input():
print '**', line

and found that it would print nothing until I hit Ctl-D, then print
all the lines, then wait for another Ctl-D, and so on (until I pressed
Ctl-D twice in succession to end the loop).

Note that you are doing a lot more than just reading from a file here. This
is calling a function in a module. Fortunately, you have the source to
look at.

As I read the fileinput.py module, this will eventually call
FileInput.next(), which eventually calls FileInput.readline(), which
eventually calls stdin.readlines(_bufsize). The default buffer size is
8,192 bytes.
OK, I think I've got this figured out!

I'll keep using fileinput.input() for the normal case (reading from
files named as arguments) and use sys.stdin.readline() for testing
(reading from the keyboard, when no filenames are given).

Thanks to you and Jonathan for your advice.
Dec 21 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Daniel Yoo | last post by:
Hi everyone, I'm was wondering: would it be a good idea to have the FileInput class support a read() method? I found myself running into a small problem while using xml.sax.parse in combination...
3
by: Chris Connett | last post by:
I just started working with the fileinput module, and reading the bit about inplace modification, it seems very dangerous: From the doc: --- Optional in-place filtering: if the keyword argument...
1
by: Scott Shaw | last post by:
Hi all, I was wondering if you could help out with this problem that I am having. What I am trying to do is detect keyboard input in a while loop without halting/pausing the loop until the key is...
5
by: the.theorist | last post by:
I was writing a small script the other day with the following CLI prog * I've used getopt to parse out the possible options, so we'll ignore that part, and assume for the rest of the discussion...
6
by: cyberco | last post by:
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...
10
by: wo_shi_big_stomach | last post by:
Newbie to python writing a script to recurse a directory tree and delete the first line of a file if it contains a given string. I get the same error on a Mac running OS X 10.4.8 and FreeBSD 6.1. ...
0
by: Phoe6 | last post by:
Hi All, I am able to use urlib2 through proxy. I give proxy credentials and use # Set the Proxy Address proxy_ip = "10.0.1.1:80" proxy_user = 'senthil_or' proxy_password_orig='password'
11
by: jo3c | last post by:
hi everybody im a newbie in python i need to read line 4 from a header file using linecache will crash my computer due to memory loading, because i am working on 2000 files each is 8mb ...
3
by: Robert | last post by:
I would like to count lines in a file using the fileinput module and I am getting an unusual output. ------------------------------------------------------------------------------...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.