A completely silly question

A

Amir Dekel

This must be the silliest question ever:

What about user input in Python? (like stdin)
Where can I find it? I can't find any references to it in the documentation.

Amir
 
F

Frans Englich

This must be the silliest question ever:

What about user input in Python? (like stdin)
Where can I find it? I can't find any references to it in the
documentation.

See sys.stdin


Cheers,

Frans
 
H

Harlin Seritt

Amir said:
This must be the silliest question ever:

What about user input in Python? (like stdin)
Where can I find it? I can't find any references to it in the
documentation.

Amir

Simple, Simple, Simple:

Var = raw_input("Some prompting text here: ")
 
A

Amir Dekel

Harlin said:
Simple, Simple, Simple:

Var = raw_input("Some prompting text here: ")

Frans said:
>
> See sys.stdin
>

What I need from the program is to wait for a single character input,
something like while(getchar()) in C. All those Python modules don't
make much sence to me...

Amir
 
S

Steven Bethard

Amir said:
What I need from the program is to wait for a single character input,
something like while(getchar()) in C. All those Python modules don't
make much sence to me...

sys.stdin.read(1)

but if you're having trouble reading the module documentation, maybe you
could elaborate on what's giving you trouble. The answer to 99% of
questions on this list is in the documentation somewhere, so if you
don't know how to read it, you're going to have trouble with Python.

Steve
 
P

Peter Hansen

Amir said:
What I need from the program is to wait for a single character input,
something like while(getchar()) in C.

Try the "msvcrt" module if you are on Windows.

If you are not, remember to specify your platform next time
you ask a question...
All those Python modules don't
make much sence to me...

That's going to make it hard to program in Python, I suspect.
Maybe it would be helpful to run through the tutorial, or
spend more time reading the docs.

-Peter
 
M

Mike Meyer

Steven Bethard said:
sys.stdin.read(1)

That doesn't do what he wants, because it doesn't return until you hit
a newline.

The answer is system dependent. Or you can use massive overkill and
get curses, but if you're on windows you'll have to use a third party
curses package, and maybe wrap it
 
W

wes weston

Amir said:
What I need from the program is to wait for a single character input,
something like while(getchar()) in C. All those Python modules don't
make much sence to me...

Amir
Amir,

wes
 
S

Steven Bethard

Mike said:
That doesn't do what he wants, because it doesn't return until you hit
a newline.

Are you sure that's not just an artifact of how your terminal buffers
data for sys.stdin?

$ cat temp.py
import sys
char = sys.stdin.read(1)
while char:
print char
char = sys.stdin.read(1)
$ cat temp.txt
abc
def
$ python temp.py < temp.txt
a
b
c


d
e
f

Of course if the intent is to have this work with terminal input, then
yes, sys.stdin.read(1) is probably not going to do the right thing...

Steve
 
M

Mike Meyer

Steven Bethard said:
Of course if the intent is to have this work with terminal input, then
yes, sys.stdin.read(1) is probably not going to do the right thing...

That's what the OP asked for - a way to wait for a single character,
like while(getchar()) in C.

Hmm. That tells me he's probably on a Windows box, so my unix solution
wouldn't do him much good.

<mike
 
D

David Bolen

Mike Meyer said:
That doesn't do what he wants, because it doesn't return until you hit
a newline.

Well, but that's true as well for getchar() (at least in many cases of
interactive input and line buffering), so in that respect I do think
it's a fairly direct replacement, depending on how the OP was going to
use getchar() in the application.

For example, compare: with:

main() ... c = sys.stdin.read(1)
{ ... print ord(c),
while (1) { ...
int ch = getchar();
printf("%d ",ch);
}
}

When run, both produce (at least for me):

0123456789 (hit Enter here)
48 49 50 51 52 53 54 55 56 57 10

under both Unix (at least FreeBSD/Linux in my quick tests) and Windows
(whether MSVC or Cygwin/gcc).

(I don't include any output buffer flushing, since it shouldn't be
needed on an interactive terminal, but you could add that to ensure
that it isn't the output part that is being buffered - I did try it
just to be sure on the Unix side)
The answer is system dependent. Or you can use massive overkill and
get curses, but if you're on windows you'll have to use a third party
curses package, and maybe wrap it

If you want to guarantee you'll get the next console character without
any waiting under Windows there's an msvcrt module that contains
functions like kbhit() and getch[e] that would probably serve.

-- David
 
M

Mike Meyer

David Bolen said:
Well, but that's true as well for getchar() (at least in many cases of
interactive input and line buffering), so in that respect I do think
it's a fairly direct replacement, depending on how the OP was going to
use getchar() in the application.

The OP said "wait for a single character input". sys.stdin.read(1)
waits for a newline.

<mike
 
F

Fredrik Lundh

Well, but that's true as well for getchar() (at least in many cases of
The OP said "wait for a single character input". sys.stdin.read(1)
waits for a newline.

in the same same sentence, the OP also said that he wanted something like
C's getchar(). if you guys are going to read only parts of the original post,
you could at least try to read an entire sentence, before you start arguing...

</F>
 
C

Craig Ringer

This must be the silliest question ever:

What about user input in Python? (like stdin)
Where can I find it? I can't find any references to it in the documentation.

Under UNIX, I generally either use curses, or just put the terminal into
raw mode:

..>>> def sane():
..... os.system("stty sane")
.....
..>>> def raw():
..... os.system("stty raw")
.....
..>>> raw()
..>>> x = sys.stdin.read(1)
a
..>>> sane()
..>>> x
'a'

but that's more for the benefit of others here, since you're on Windows.
Needless to say this isn't portable.

It can often be worth also using the 'echo' and 'noecho' arguments to
stty to prevent characters getting echoed in ugly places. If you do much
of this, it's probably worth just using curses, but if you have a fairly
basic app that just needs to read raw characters sometimes this approach
should be fine.
 
M

Mike Meyer

Craig Ringer said:
Under UNIX, I generally either use curses, or just put the terminal into
raw mode:

.>>> def sane():
.... os.system("stty sane")
....
.>>> def raw():
.... os.system("stty raw")

The termios gives module gives you the tools to manipulate the tty
directly, without invoking stty. The tty module gives you an easier
interface to those routines. However, it's missing a setsane
functions. Hmm. I think it's time for another PEP.

<mike
 
K

Keith Dart

Mike said:
The termios gives module gives you the tools to manipulate the tty
directly, without invoking stty. The tty module gives you an easier
interface to those routines. However, it's missing a setsane
functions. Hmm. I think it's time for another PEP.

<mike

In the pyNMS package (http://sourceforge.net/projects/pynms/) there is a
module called "termtools". This module can be used in place of the "tty"
module. It has many improvements, including a "sane" function, a "raw"
function, and an "stty" function. This module also replaces the
"getpass" module, as it has the same functions found there. The PagedIO
object is used by the CLI framework in pyNMS.
 
D

David Bolen

Fredrik Lundh said:
in the same same sentence, the OP also said that he wanted something like
C's getchar(). if you guys are going to read only parts of the original post,
you could at least try to read an entire sentence, before you start arguing...

Not even sure what's there to argue about - getchar() does do single
character input, so the OPs (full) original sentence seems plausible
to me, and his example was using it in a while loop which I took to
represent processing some input one character at a time.

In any event - I also gave a way (Windows-specific) to truly obtain
the single next character without any buffering, so just ignore any
controversy in the first part of the response if desired.

-- David
 

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
474,214
Messages
2,571,112
Members
47,705
Latest member
noname22

Latest Threads

Top