473,769 Members | 2,394 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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"|pyth on 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,somethi ng like....

#someprog.py
import sys
print "hello"|sys.std in.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 7475
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"|pyth on 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,somethi ng like....

#someprog.py
import sys
print "hello"|sys.std in.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.std in.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
6297
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", stdin=subprocess.PIPE) # Run an octave called "startup". oct.communicate("startup") # Change directory inside octave.
7
2228
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
19571
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
6345
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 programs: /*** a.c ***/
2
3205
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 what I've read so far, I've figured that I need to do something like: f_in, f_out = popen4("python -u script.py") where I have extracted the text from the editor and put it in "script.py". (However, see below).
13
3160
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 necessary, why, and what their alternative is in Python?
5
1210
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 so I used Python. One of the main rules is, that the code reads its standard input and dumps the result on the standard output. Here happened my bigger programming mistake ever - I used the following approach : ====
5
14330
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 consistently gets an "invalid operand" IOError when write() is called on the pipe. So I switched to subprocess. It works fine when executed "normally" (e.g. "python progname.py"), but when bundled by py2exe, it always does this:
0
1996
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 Python it's easy, use the ps1 prompt. --- begin --- import sys
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10219
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.