Perl to Python
Andrew Dalke
dalke at dalkescientific.com
Tue Nov 27 15:21:00 EST 2001
Tim Hammerquist:
>Pipes can be opened using os.popen(). Pipes can be read/written by
>reading from/writing to the file object returned by os.popen(). Read
>from this as normal in Python, just as you're reading from the SQL
>filehandle in the perl code.
>
> # Perl
> open PIPE, "ls /etc" or die "can't read from pipe: $!\n";
> print while <PIPE>;
> close PIPE;
>
> # Python (roughly)
> pipe = os.popen('ls /etc')
> while 1:
> line = pipe.readline()
> if not line:
> break
> print line
> retval = pipe.close()
The Python isn't quite equivalent to the Perl since 'print' adds
the newline after printing. You'll either need to chomp off the
final newline or use sys.stdout.write(line).
Assuming Python 2.2 iterators, this can be written
pipe = os.popen('ls /etc')
for line in pipe:
sys.stdout.write(line)
retval = pipe.close()
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list