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

Some general questions about using "stdin","stdout"....

Hi!I'm new in Python and i'd like to ask some general questions about
stdin,stdout...

Firstly...

if we type like something like :
cat "file.txt"|python somefile.py

#somefile.py
import sys
text=sys.stdin.read()
....then "sys.stdin.read()" will read from "cat"s stdout...
However,if i type inside a program,something like....

#someprog.py
import sys
print "hello"|sys.stdin.read()

......the screen hangs..why is that?isn't the same situation as "cat"?

in addition to this...
Why can't i "write" to the stdin?
Isn't it being used as a file object like all the others?
for example
sys.stdin.close() or
open('sys.stdin','w+') or
sys.stdin.write("something") etc... don't work...

At last,let's consider "sys.stdin.read(3)"
If i type that,the program "waits" for me to type some characters,and then
prints the first three...
However,doesn't this action actually include a "writing" to stdin and then
a "reading" from that?

I'm really confused...
Any help would be highly appreciated...
Thanks a lot.

__________________________________________________ _______________
Free blogging with MSN Spaces http://spaces.msn.com/?mkt=nl-be

Feb 16 '06 #1
1 7457
asdsd sir wrote:
Hi!I'm new in Python and i'd like to ask some general questions about
stdin,stdout...

Firstly...

if we type like something like :
cat "file.txt"|python somefile.py

#somefile.py
import sys
text=sys.stdin.read()
...then "sys.stdin.read()" will read from "cat"s stdout...
However,if i type inside a program,something like....

#someprog.py
import sys
print "hello"|sys.stdin.read()

.....the screen hangs..why is that?isn't the same situation as "cat"?
Obviously not... The stdin is a read-only file that you can read from the
data that is passed via a pipe to your application. You did tha piping by
using

cat "file.txt" | python somefile.py

That establihes the pipe between cat's stdout(!) and python's stdin. That's
the reason that | is called "pipe" or "pipe-operator" in the SHELL(!)

print "hello"|sys.stdin.read()

OTH is inside python, and | is not the pipe-operator, but the binary
or-operator. Consider this:
print 1 | 2 3

But: print "hello" | "some other string"

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for |: 'str' and 'str'
So you don't write something to stdin by that. Instead it waits endlessly,
trying to read something that is piped to it from the outside. But if that
was the case, it would puke on you with the above error message.
in addition to this...
Why can't i "write" to the stdin?
Isn't it being used as a file object like all the others?
for example
sys.stdin.close() or
open('sys.stdin','w+') or
sys.stdin.write("something") etc... don't work...


Because it is defined that way. I suggest you read up on unix piping to
grasp the concepts behind it - python only follows these.
Diez
Feb 16 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Edward C. Jones | last post by:
I have a program named "octave" (a Matlab clone). It runs in a terminal, types a prompt and waits for the user to type something. If I try # Run octave. oct = subprocess.Popen("octave",...
7
by: Xah Lee | last post by:
does anyone know why the folllowing prints to the screen? # python import os os.system(r"ls") Xah xah@xahlee.org ∑ http://xahlee.org/
2
by: Todd Nathan | last post by:
Hi. have this code and compiler problem. GCC 2.95.3, BeOS, error "initializer element is not constant" #ifdef FILEIO { static struct { char *sfn; FILE *sfd; } stdfiles = {
145
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two...
2
by: André | last post by:
Short description: Using a wxPython based app, I would like to take a python script in an editor window, invoke the python interpreter and use another window as stdin/stdout/stderr. Based on...
13
by: Vincent Delporte | last post by:
Hi I'm a Python newbie, and would like to rewrite this Perl scrip to be run with the Asterisk PBX: http://www.voip-info.org/wiki/view/Asterisk+NetCID Anyone knows if those lines are...
5
by: stdazi | last post by:
Hello, Yesterday, I was at a programming competition. We programmed on Linux liveCD's and Python was one of the allowed languages (among C and Java). I cared just about the algorithmic approach...
5
by: Grant Edwards | last post by:
I'm trying to use the py-gnuplot module on windows, and have been unable to get it to work reliably under Win2K and WinXP. By default, it uses popen(gnuplotcmd,'w'), but in some situations that...
0
by: Gabriel Genellina | last post by:
En Thu, 25 Sep 2008 09:49:31 -0300, Almar Klein <almar.klein@gmail.com> escribió: Use subprocess.PIPE Usually the tricky part is to figure out exactly whether there is more input or not. With...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.