Perl to Python
Andrew Dalke
dalke at dalkescientific.com
Tue Nov 27 15:21:00 EST 2001
More information about the Python-list mailing list
Tue Nov 27 15:21:00 EST 2001
- Previous message (by thread): Perl to Python
- Next message (by thread): Perl to Python
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Previous message (by thread): Perl to Python
- Next message (by thread): Perl to Python
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list