popen function of os and subprocess modules

banu varun.nagpaal at gmail.com
Wed Oct 28 09:39:30 EDT 2009


Hi,
I am a novice in python. I was trying to write a simple script on
Linux (python 3.0) that does the following

#cd directory
#ls -l

I use the following code, but it doesn't work:

import os
directory = '/etc'
pr = os.popen('cd %s' % directory,'w')
pr.close()
pr = os.popen('ls -l','w')                                      #
prints the content of present folder and not '/etc'
pr.close()

Can anyone suggest me how to fix this simple script? Also what is the
use of read(), readlines() and write() functions?

Now, I also read in the online python documentation that os.popen is
deprecated and no longer recommended in pyhton 3.0. Instead they ask
to use subprocess.popen. I am not able to figure out how to accomplish
my task with subprocess.poepn also. Can anyone suggest please?


Regards
Varun



More information about the Python-list mailing list