os.popen and place in file
Stephen Boulet
stephen at theboulets.net
Wed Jun 5 12:59:52 EDT 2002
Thanks to all for the responses. Here's what I was able to do, using the
StringIO module/class:
>>> import os
>>> from StringIO import *
>>> f = os.popen('ls /usr/local/bin')
>>> f = f.read()
>>> f=StringIO(f)
>>> for i in range(5):
... print f.readline()
...
acroread
audacity
cabextract
demo
dos2unix
>>> f.seek(0)
>>> for i in range(5):
... print f.readline()
...
acroread
audacity
cabextract
demo
dos2unix
-- Stephen
Erno Kuusela wrote:
> In article <3CFE2762.519E4344 at motorola.com>, Stephen Boulet
> <stephen.boulet at motorola.com> writes:
>
> | I'm running windows NT.
> | The command:
> | f = os.popen('dir')
> | returns a file object 'f'. I can't however use the seek command on it
> | [...] Anyone know why?
>
> it is because the text output by the "dir" program is relayed directly
> to your program. the operating system does not store it away anywhere
> in case you want to revisit parts of it later. sockets and many
> unix device files work the same way. otherwise there would
> be problems when transferring large amounts of data through
> pipes.
>
> if you want to access the data like a regular file, you could redirect
> the output from the command to a disk file and open that. or, read
> all of the data from the pipe and put it in a StringIO object.
>
> -- erno
More information about the Python-list
mailing list