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

A completely silly question

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a
On Friday 17 December 2004 16:40, Amir Dekel wrote:
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
Jul 18 '05 #2

P: n/a
Amir Dekel wrote:
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: ")
--
Harlin Seritt
Jul 18 '05 #3

P: n/a
Harlin Seritt wrote:

Simple, Simple, Simple:

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

Frans Englich wrote:
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
Jul 18 '05 #4

P: n/a
Amir Dekel wrote:
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
Jul 18 '05 #5

P: n/a
Amir Dekel wrote:
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
Jul 18 '05 #6

P: n/a
Steven Bethard <st************@gmail.com> writes:
Amir Dekel wrote:
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)


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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #7

P: n/a
Amir Dekel wrote:
Harlin Seritt wrote:

Simple, Simple, Simple:

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


Frans Englich wrote:
>
> 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


Amir,
import tkSimpleDialog
ch = tkSimpleDialog.askstring("","ch?")


wes

Jul 18 '05 #8

P: n/a
Amir Dekel wrote:

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...


Take a look at Alan Gauld's "Learning to Program"
(http://www.freenetpages.co.uk/hp/alan.gauld/)
in the section "Event Driven programming"

Hope it helps

Marco
Jul 18 '05 #9

P: n/a
Mike Meyer wrote:
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
Jul 18 '05 #10

P: n/a
Steven Bethard <st************@gmail.com> writes:
Mike Meyer wrote:
That doesn't do what he wants, because it doesn't return until you hit
a newline.

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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #11

P: n/a
Mike Meyer wrote:

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

Yes, Windows...too bad
Jul 18 '05 #12

P: n/a
Mike Meyer <mw*@mired.org> writes:
Steven Bethard <st************@gmail.com> writes:
Amir Dekel wrote:
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)


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:

#include <stdio.h> >>> import sys
while 1:

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
Jul 18 '05 #13

P: n/a
David Bolen <db**@fitlinxx.com> writes:
Mike Meyer <mw*@mired.org> writes:
Steven Bethard <st************@gmail.com> writes:
> Amir Dekel wrote:
>> 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)


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.


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

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #14

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


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>

Jul 18 '05 #15

P: n/a
On Sat, 2004-12-18 at 00:40, Amir Dekel wrote:
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.

--
Craig Ringer
Jul 18 '05 #16

P: n/a
Craig Ringer <cr***@postnewspapers.com.au> writes:
On Sat, 2004-12-18 at 00:40, Amir Dekel wrote:
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")


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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #17

P: n/a
Mike Meyer wrote:
Craig Ringer <cr***@postnewspapers.com.au> writes:

On Sat, 2004-12-18 at 00:40, Amir Dekel wrote:
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")

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.


--
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~
Keith Dart <kd***@kdart.com>
public key: ID: F3D288E4
================================================== ===================
Jul 18 '05 #18

P: n/a
"Fredrik Lundh" <fr*****@pythonware.com> writes:
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.


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
Jul 18 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.