popen function of os and subprocess modules
joncle at googlemail.com
Wed Oct 28 15:02:45 CET 2009
On 28 Oct, 13:39, banu <varun.nagp... at gmail.com> wrote:
> 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 = os.popen('ls -l','w') #
> prints the content of present folder and not '/etc'
> 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?
If you're only trying to get the contents of a directory, there are
more suitable functions - you don't need a separate process. The popen*
() commands are deprecated.
Try using os.listdir() - can't remember off the top of my head if
that's been moved to os.path.listdir() in the 3.* series, but a read
of the doc's will set you straight.
Ditto for read() and write().
If you describe what you're trying to achieve, maybe we can help more.
Also, if you're using 3.0, may I suggest moving to 3.1?
More information about the Python-list