473,320 Members | 1,746 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,320 software developers and data experts.

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
Jul 18 '05 #1
18 1679
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
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
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
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
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
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
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
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
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
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
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
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
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
>> 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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Pjotr Wedersteers | last post by:
This may be a silly question, but I was wondering. If 10 clients fill out a form on my page simultaneaously and hit submit, how does my (Apache2.0.50/PHP4.3.8) server exactly ensure each gets...
4
by: Jenny | last post by:
Hi you al I have two very silly question The first one is In vb 6.0 you can add a procedure, sub or function, by clicking add procedure from the tools item on the menuba I cannot seem to find...
4
by: musosdev | last post by:
I think I'm having a dim day and should just go back to bed, but alas, I cant do that.... I'm writing a peice of code to create XHTML compliant documents using System.IO, there's probably an...
6
by: Adam Honek | last post by:
Hi, I've looked over and over and in MSDN 2005 and just can't find what is a really simple answer. How do I select the first value in a combo box to be shown? I tried the .selectedindex -1...
7
by: Matt | last post by:
I've asked this question to some developers that are much more experienced than I, and gotten different answers --- and I can't find anything about it in the documentation. Dim vs. Private as a...
2
by: Willem Voncken | last post by:
Hi guys, might be a silly question, but i'm wondering whether it is even possible to generate access violations when using C#? I'm new at c# programming, but i have some experience with c++....
1
by: Pontifex | last post by:
Hi all, Has anyone devised a simple method for passing a string to an external script? For instance, suppose I want to produce a title bar that is very fancy and I don't want my main HTML...
4
by: Marcin Kasprzak | last post by:
Hello Guys, Silly question - what is the most elegant way of compiling a code similar to this one? <code> typedef struct a { b_t *b; } a_t; typedef struct b {
5
by: Ben Bacarisse | last post by:
Newbie <newbie@gmail.comwrites: <snip> The version you posted in comp.programming has the correct loop. Why change it? -- Ben.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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...
1
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.