[Tutor] Unix commands python

Karl Pflästerer sigurd at 12move.de
Tue Apr 27 13:42:39 EDT 2004


On 27 Apr 2004, Kooser, Ara S <- askoose at sandia.gov wrote:

>   I am new at python and I am working on a file conversion program. Thank
> you for all the advice that got me to this point. Is it possible to invoke
> unix commands in python, such as awk or cut? I am trying to cut some columns
> of text and then transform one of those columns with user input. Thanks. 

Yes it's possible.  Look for os.popen, os.popen2, os.popen3, os.popen4
E.g to view only the first column of a ls -l (a silly example):

>>> ls = os.popen('ls -l|cut -d " " -f 1')
>>> for line in ls:
...     sys.stdout.write(line)
... 
total
-rw-r--r--
>>> 


os.popen() returns the output from the command as file object; reading
is the default mode but you can also open it for writing (if it makes
sense) the same way you do it with files.  Perhaps the best is you look
it up in the library documentation.


   Karl
-- 
Please do *not* send copies of replies to me.
I read the list




More information about the Tutor mailing list