stdin -> stdout

Discussion in 'Python' started by max(01)*, Aug 19, 2005.

  1. max(01)*

    max(01)* Guest

    hi.

    i was wondering, what's the simplest way to echo the standard input to
    the standard output, with no modification.

    i came up with:

    ....
    while True:
    try:
    raw_input()
    except EOFError:
    break
    ....

    but i guess there must be a simpler way.

    using bash i simply do 'cat', *sigh*!

    bye

    max

    ps: in perl you ca do this:

    ....
    while ($line = <STDIN>)
    {
    print STDOUT ("$line");
    }
    ....
    max(01)*, Aug 19, 2005
    #1
    1. Advertising

  2. max(01)*

    limodou Guest

    2005/8/19, max(01)* <>:
    > hi.
    >
    > i was wondering, what's the simplest way to echo the standard input to
    > the standard output, with no modification.
    >
    > i came up with:
    >
    > ...
    > while True:
    > try:
    > raw_input()
    > except EOFError:
    > break
    > ...
    >
    > but i guess there must be a simpler way.
    >
    > using bash i simply do 'cat', *sigh*!
    >
    > bye
    >
    > max
    >
    > ps: in perl you ca do this:
    >
    > ...
    > while ($line = <STDIN>)
    > {
    > print STDOUT ("$line");
    > }
    > ...


    Try this.

    import sys

    line = sys.stdin.readline()
    while line:
    sys.stdout.write(line)
    line = sys.stdin.readline()

    --
    I like python!
    My Donews Blog: http://www.donews.net/limodou
    limodou, Aug 19, 2005
    #2
    1. Advertising

  3. max(01)*

    Dan Sommers Guest

    On Fri, 19 Aug 2005 15:26:27 GMT,
    "max(01)*" <> wrote:

    > ps: in perl you ca do this:


    > ...
    > while ($line = <STDIN>)
    > {
    > print STDOUT ("$line");
    > }
    > ...


    import fileinput
    import sys

    for line in fileinput.input():
    sys.stdout.write(line)

    Regards,
    Dan

    --
    Dan Sommers
    <http://www.tombstonezero.net/dan/>
    Dan Sommers, Aug 19, 2005
    #3
  4. max(01)*

    Guest

    import sys
    for l in sys.stdin:
    sys.stdout.write(l)

    -- George
    , Aug 19, 2005
    #4
  5. max(01)* wrote:
    > i was wondering, what's the simplest way to echo the standard input to
    > the standard output, with no modification.


    import sys
    for line in iter(sys.stdin.readline, ''):
    sys.stdout.write(line)

    Note that this uses the second form of iter(), which calls its first
    argument repeatedly until it returns the sentinel value (its second
    argument).

    STeVe
    Steven Bethard, Aug 19, 2005
    #5
  6. wrote:
    > import sys
    > for l in sys.stdin:
    > sys.stdout.write(l)


    This is fine if you don't need the reads and writes of lines to run in
    lockstep. File iterators read into a buffer, so you'll probably read
    4096 bytes from stdin before you ever write a line to stdout. If that's
    okay, this is a good solution. OTOH, if you want the reads and writes
    to run in lockstep, you should probably use this idiom:

    import sys
    for line in iter(sys.stdin.readline, ''):
    sys.stdout.write(line)

    STeVe

    P.S. You may also be able to get your version working using the -u
    (unbuffered) option to Python, but I couldn't.
    Steven Bethard, Aug 19, 2005
    #6
  7. max(01)*

    Jorgen Grahn Guest

    On Fri, 19 Aug 2005 15:26:27 GMT, max(01)* <> wrote:
    > hi.
    >
    > i was wondering, what's the simplest way to echo the standard input to
    > the standard output, with no modification.
    >
    > i came up with:

    ....
    > but i guess there must be a simpler way.
    >
    > using bash i simply do 'cat', *sigh*!

    ....
    > ps: in perl you ca do this:
    >
    > ...
    > while ($line = <STDIN>)
    > {
    > print STDOUT ("$line");
    > }
    > ...


    Actually, in perl it's easier than that, if you can tolerate that it also
    filters file(s) given on the command-line:

    while(<>) {
    print;
    }

    or even:

    #!/usr/bin/perl -p
    ;

    Of course, not every programming language needs to have easy-to-use
    filtering capabilities at the core.

    /Jorgen

    --
    // Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
    \X/ algonet.se> R'lyeh wgah'nagl fhtagn!
    Jorgen Grahn, Aug 20, 2005
    #7
  8. max(01)*

    Jeff Schwab Guest

    max(01)* wrote:
    > i was wondering, what's the simplest way to echo the standard input to
    > the standard output, with no modification.

    ....
    > ps: in perl you ca do this:
    >
    > ...
    > while ($line = <STDIN>)
    > {
    > print STDOUT ("$line");
    > }
    > ...


    I guess you could, but there wouldn't be much point. In Perl, you can
    do this with just command-line flags:

    perl -pe '' input.txt

    Or if you really want something to put in a file:

    print <>

    Here's the closest thing I could come up with in Python:

    import sys
    for line in sys.stdin:
    print line,
    Jeff Schwab, Aug 20, 2005
    #8
    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,959
    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:
    786
    Dan Pop
    Jun 21, 2004
  3. Ben
    Replies:
    2
    Views:
    1,343
    jacob navia
    Aug 29, 2009
  4. Terry Cooper
    Replies:
    7
    Views:
    424
    Janos Sebok
    Jun 9, 2009
  5. Stefano Sabatini
    Replies:
    6
    Views:
    292
    Stefano Sabatini
    Jul 29, 2007
Loading...

Share This Page