confused about stdin

Discussion in 'Python' started by David Bear, Oct 22, 2004.

  1. David Bear

    David Bear Guest

    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"
    David Bear, Oct 22, 2004
    #1
    1. Advertising

  2. David Bear

    Steve Holden Guest

    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
    Steve Holden, Oct 22, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johnathan Doe

    peek at stdin, flush stdin

    Johnathan Doe, May 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    24,773
    Chatoyer
    May 17, 2013
  2. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    756
    Dan Pop
    Jun 21, 2004
  3. Ben
    Replies:
    2
    Views:
    1,314
    jacob navia
    Aug 29, 2009
  4. Terry Cooper
    Replies:
    7
    Views:
    411
    Janos Sebok
    Jun 9, 2009
  5. Stefano Sabatini
    Replies:
    6
    Views:
    274
    Stefano Sabatini
    Jul 29, 2007
Loading...

Share This Page