A completely silly question | | |
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 | | | | re: A completely silly question
On Friday 17 December 2004 16:40, Amir Dekel wrote:[color=blue]
> 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.[/color]
See sys.stdin
Cheers,
Frans | | | | re: A completely silly question
Amir Dekel wrote:
[color=blue]
> 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[/color]
Simple, Simple, Simple:
Var = raw_input("Some prompting text here: ")
--
Harlin Seritt | | | | re: A completely silly question
Harlin Seritt wrote:
[color=blue]
>
> Simple, Simple, Simple:
>
> Var = raw_input("Some prompting text here: ")
>[/color]
Frans Englich wrote:[color=blue]
>
> See sys.stdin
>[/color]
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 | | | | re: A completely silly question
Amir Dekel wrote:[color=blue]
> 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...[/color]
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 | | | | re: A completely silly question
Amir Dekel wrote:[color=blue]
> What I need from the program is to wait for a single character input,
> something like while(getchar()) in C.[/color]
Try the "msvcrt" module if you are on Windows.
If you are not, remember to specify your platform next time
you ask a question...
[color=blue]
> All those Python modules don't
> make much sence to me...[/color]
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 | | | | re: A completely silly question
Steven Bethard <steven.bethard@gmail.com> writes:
[color=blue]
> Amir Dekel wrote:[color=green]
>> 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...[/color]
>
> sys.stdin.read(1)[/color]
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 <mwm@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. | | | | re: A completely silly question
Amir Dekel wrote:[color=blue]
> Harlin Seritt wrote:
>[color=green]
>>
>> Simple, Simple, Simple:
>>
>> Var = raw_input("Some prompting text here: ")
>>[/color]
>
> Frans Englich wrote:[color=green]
> >
> > See sys.stdin
> >[/color]
>
> 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[/color]
Amir,
[color=blue][color=green][color=darkred]
>>> import tkSimpleDialog
>>> ch = tkSimpleDialog.askstring("","ch?")[/color][/color][/color]
wes | | | | re: A completely silly question
Amir Dekel wrote:
[color=blue]
>
> 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...[/color]
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 | | | | re: A completely silly question
Mike Meyer wrote:[color=blue]
> That doesn't do what he wants, because it doesn't return until you hit
> a newline.[/color]
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 | | | | re: A completely silly question
Steven Bethard <steven.bethard@gmail.com> writes:
[color=blue]
> Mike Meyer wrote:[color=green]
>> That doesn't do what he wants, because it doesn't return until you hit
>> a newline.[/color]
> 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...[/color]
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 <mwm@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. | | | | re: A completely silly question
Mike Meyer wrote:
[color=blue]
>
> Hmm. That tells me he's probably on a Windows box, so my unix solution
> wouldn't do him much good.
>[/color]
Yes, Windows...too bad | | | | re: A completely silly question
Mike Meyer <mwm@mired.org> writes:
[color=blue]
> Steven Bethard <steven.bethard@gmail.com> writes:
>[color=green]
> > Amir Dekel wrote:[color=darkred]
> >> 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...[/color]
> >
> > sys.stdin.read(1)[/color]
>
> That doesn't do what he wants, because it doesn't return until you hit
> a newline.[/color]
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[color=blue][color=green][color=darkred]
>>> while 1:[/color][/color][/color]
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)
[color=blue]
> 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[/color]
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 | | | | re: A completely silly question
David Bolen <db3l@fitlinxx.com> writes:
[color=blue]
> Mike Meyer <mwm@mired.org> writes:
>[color=green]
>> Steven Bethard <steven.bethard@gmail.com> writes:
>>[color=darkred]
>> > 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)[/color]
>>
>> That doesn't do what he wants, because it doesn't return until you hit
>> a newline.[/color]
>
> 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.[/color]
The OP said "wait for a single character input". sys.stdin.read(1)
waits for a newline.
<mike
--
Mike Meyer <mwm@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. | | | | re: A completely silly question
>> Well, but that's true as well for getchar() (at least in many cases of[color=blue][color=green]
>> 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.[/color]
>
> The OP said "wait for a single character input". sys.stdin.read(1)
> waits for a newline.[/color]
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> | | | | re: A completely silly question
On Sat, 2004-12-18 at 00:40, Amir Dekel wrote:[color=blue]
> 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.[/color]
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 | | | | re: A completely silly question
Craig Ringer <craig@postnewspapers.com.au> writes:
[color=blue]
> On Sat, 2004-12-18 at 00:40, Amir Dekel wrote:[color=green]
>> 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.[/color]
>
> 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")[/color]
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 <mwm@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. | | | | re: A completely silly question
Mike Meyer wrote:[color=blue]
> Craig Ringer <craig@postnewspapers.com.au> writes:
>
>[color=green]
>>On Sat, 2004-12-18 at 00:40, Amir Dekel wrote:
>>[color=darkred]
>>>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.[/color]
>>
>>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")[/color]
>
>
> 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[/color]
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 <kdart@kdart.com>
public key: ID: F3D288E4
================================================== =================== | | | | re: A completely silly question
"Fredrik Lundh" <fredrik@pythonware.com> writes:
[color=blue][color=green][color=darkred]
> >> 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.[/color]
> >
> > The OP said "wait for a single character input". sys.stdin.read(1)
> > waits for a newline.[/color]
>
> 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...[/color]
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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|