confused about stdin

Steve Holden steve at holdenweb.com
Thu Oct 21 20:05:51 EDT 2004


David Bear wrote:

> 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
-- 
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119



More information about the Python-list mailing list