Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > stdin -> stdout

Reply
Thread Tools

stdin -> stdout

 
 
max(01)*
Guest
Posts: n/a
 
      08-19-2005
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");
}
....
 
Reply With Quote
 
 
 
 
limodou
Guest
Posts: n/a
 
      08-19-2005
2005/8/19, max(01)* <(E-Mail Removed)>:
> 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
 
Reply With Quote
 
 
 
 
Dan Sommers
Guest
Posts: n/a
 
      08-19-2005
On Fri, 19 Aug 2005 15:26:27 GMT,
"max(01)*" <(E-Mail Removed)> 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/>
 
Reply With Quote
 
gry@ll.mit.edu
Guest
Posts: n/a
 
      08-19-2005
import sys
for l in sys.stdin:
sys.stdout.write(l)

-- George

 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      08-19-2005
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
 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      08-19-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      08-20-2005
On Fri, 19 Aug 2005 15:26:27 GMT, max(01)* <(E-Mail Removed)> 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!
 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      08-20-2005
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,
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
peek at stdin, flush stdin Johnathan Doe C Programming 5 05-17-2013 04:30 PM
How to pass stdin of a C++ program to the stdin of a process createdwith ShellExecute() Ben C Programming 2 08-29-2009 09:47 PM
STDIN, OUT, ERR and $stdin, out, err - Differences? Terry Cooper Ruby 7 06-09-2009 05:48 AM
Reading from stdin then launching a program that reads from stdin strange behaviour Stefano Sabatini Perl Misc 6 07-29-2007 10:38 PM
Reading stdin once confuses second stdin read Charlie Zender C Programming 6 06-21-2004 01:39 PM



Advertisments