stdin -> stdout

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • max(01)*

    #1

    stdin -> stdout

    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");
    }
    ....
  • limodou

    #2
    Re: stdin -&gt; stdout

    2005/8/19, max(01)* <max2@fisso.cas a>:[color=blue]
    > 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");
    > }
    > ...[/color]

    Try this.

    import sys

    line = sys.stdin.readl ine()
    while line:
    sys.stdout.writ e(line)
    line = sys.stdin.readl ine()

    --
    I like python!
    My Donews Blog: http://www.donews.net/limodou

    Comment

    • Dan Sommers

      #3
      Re: stdin -&gt; stdout

      On Fri, 19 Aug 2005 15:26:27 GMT,
      "max(01)*" <max2@fisso.cas a> wrote:
      [color=blue]
      > ps: in perl you ca do this:[/color]
      [color=blue]
      > ...
      > while ($line = <STDIN>)
      > {
      > print STDOUT ("$line");
      > }
      > ...[/color]

      import fileinput
      import sys

      for line in fileinput.input ():
      sys.stdout.writ e(line)

      Regards,
      Dan

      --
      Dan Sommers
      <http://www.tombstoneze ro.net/dan/>

      Comment

      • gry@ll.mit.edu

        #4
        Re: stdin -&gt; stdout

        import sys
        for l in sys.stdin:
        sys.stdout.writ e(l)

        -- George

        Comment

        • Steven Bethard

          #5
          Re: stdin -&gt; stdout

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

          import sys
          for line in iter(sys.stdin. readline, ''):
          sys.stdout.writ e(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

          Comment

          • Steven Bethard

            #6
            Re: stdin -&gt; stdout

            gry@ll.mit.edu wrote:[color=blue]
            > import sys
            > for l in sys.stdin:
            > sys.stdout.writ e(l)[/color]

            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.writ e(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.

            Comment

            • John Machin

              #7
              Re: stdin -&gt; stdout

              limodou wrote:[color=blue]
              > 2005/8/19, max(01)* <max2@fisso.cas a>:
              >[color=green]
              >>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");
              >> }
              >>...[/color]
              >
              >
              > Try this.
              >
              > import sys
              >
              > line = sys.stdin.readl ine()
              > while line:
              > sys.stdout.writ e(line)
              > line = sys.stdin.readl ine()
              >[/color]

              Try this:

              import sys
              for line in sys.stdin:
              sys.stdout.writ e(line)



              Comment

              • Jorgen Grahn

                #8
                Re: stdin -&gt; stdout

                On Fri, 19 Aug 2005 15:26:27 GMT, max(01)* <max2@fisso.cas a> wrote:[color=blue]
                > 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:[/color]
                ....[color=blue]
                > but i guess there must be a simpler way.
                >
                > using bash i simply do 'cat', *sigh*![/color]
                ....[color=blue]
                > ps: in perl you ca do this:
                >
                > ...
                > while ($line = <STDIN>)
                > {
                > print STDOUT ("$line");
                > }
                > ...[/color]

                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!

                Comment

                • Jeff Schwab

                  #9
                  Re: stdin -&gt; stdout

                  max(01)* wrote:[color=blue]
                  > i was wondering, what's the simplest way to echo the standard input to
                  > the standard output, with no modification.[/color]
                  ....[color=blue]
                  > ps: in perl you ca do this:
                  >
                  > ...
                  > while ($line = <STDIN>)
                  > {
                  > print STDOUT ("$line");
                  > }
                  > ...[/color]

                  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,

                  Comment

                  Working...