Printing with raw_input

P

Peter Otten

Joan said:
Does `raw_input` uses internally `sys.stdout.write`?

You can test this yourself without reading the C source:

Python 2.6.4 (r264:75706, Dec 7 2009, 18:43:55)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information..... def write(self, s):
.... sys.__stdout__.write(s.upper())
....FOObar
'BAR'

Peter
 
J

Joan Miller

Joan said:
Does `raw_input` uses internally `sys.stdout.write`?

You can test this yourself without reading the C source:

Python 2.6.4 (r264:75706, Dec  7 2009, 18:43:55)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.>>> import sys
...     def write(self, s):
...             sys.__stdout__.write(s.upper())
...>>> sys.stdout = A()
FOObar
'BAR'

Peter

It was to display the output inside a GUI app. overriding
`sys.stdout`. And as `print` also uses internally `sys.stdout.write`
then can be used `print` the shell script and get the output too in
the GUI, cann't it?
 
P

Peter Otten

It was to display the output inside a GUI app. overriding
`sys.stdout`. And as `print` also uses internally `sys.stdout.write`
then can be used `print` the shell script and get the output too in
the GUI, cann't it?

It should be easy to collect data written with print and show it in a gui,
but I can't see how you would integrate raw_input() into a gui app.

As to shell scripts, you can invoke them via subprocess, or, if the script
needs user interaction, via pexpect.

Peter
 
P

Peter Otten

Shashwat said:
raw_input uses sys.stderr I guess ?

I had a look at the C code, but it's a bit confusing.

If I'm reading it correctly the prompt is written to the "real" stderr if
and only if sys.stdin and sys.stdout are attached to a terminal.

$ python -c"raw_input('prompt\n')" 2>tmp.txt
foo
$ cat tmp.txt
prompt
$ python -c"raw_input('prompt\n')" 2>tmp.txt | cat
foo
prompt
$ cat tmp.txt

I wonder if that is intentional.

Peter
 

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

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top