confused about stdin

D

David Bear

This may be a dumb question but I'm confused.

using command line syntax pipe like this:

cat somefile | pyscript

In my pyscript I can simply do

buffer = sys.stdin.read()

to get all of whatever cat sends.

However, using syntax like this:

pyscript < somefile

I'm not sure. Does this send to stdin as well?

And.. will it work on both unix and windows?

--
David Bear
phone: 480-965-8257
fax: 480-965-9189
College of Public Programs/ASU
Wilson Hall 232
Tempe, AZ 85287-0803
"Beware the IP portfolio, everyone will be suspect of trespassing"
 
S

Steve Holden

David said:
This may be a dumb question but I'm confused.

using command line syntax pipe like this:

cat somefile | pyscript

In my pyscript I can simply do

buffer = sys.stdin.read()

to get all of whatever cat sends.
Correct. But this uses a "cat" process to very little effect.
However, using syntax like this:

pyscript < somefile

I'm not sure. Does this send to stdin as well?
Yes. Now standard input is the content of the named file, where in the
first case it was the output of the preceding process in the pipe.

The pipe case is much more useful where you are actually transforming
information rather than just using it directly, such as

sort somefile | pyscript

Since "cat" doesn't do any transformation at all, using file redirection
as in your second example is generally better practice than your first
example.
And.. will it work on both unix and windows?
Reasonably well, yes, though (at least) some Windows implementations
will use intermediate files rather than in-memory buffers to implement
the pipes, so you could end up getting less parallelism than you
anticipate.

regards
Steve
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top