popen function of os and subprocess modules

B

banu

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
 
J

Jon Clements

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

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?

hth,

Jon.
 
B

banu

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?

hth,

Jon.

Thanks for the reply Jon
Basically I need to move into a folder and then need to execute some
shell commands(make etc.) in that folder. I just gave 'ls' for the
sake of an example. The real problem I am facing is, how to stay in
the folder after popen('cd directory') finishes. It seems trivial, but
I am not able to do it.

Varun
 
B

Benjamin Kaplan

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()
So, here's what you're doing manually.
1) Open up a terminal, type "cd /etc". And then close that terminal
2) Open up a new terminal, type "ls -l" and wonder why it's not in /etc
Can anyone suggest me how to fix this simple script? Also what is the
use of read(), readlines() and write() functions?

The os and os.path modules contain higher-level functions than popen.
Such as os.listdir and os.chdir (if you really want to change the
current directory for the program).

popen returns a file object. In your case, because you opened it in
write mode, it's stdin so write will send things to the program
(assuming it reads from stdin), and read/readlines are useless. If you
were to open it in read mode, pr would be stdout and you would use
pr.read() or pr.readlines() to get your directory list instead of
having it print out to the terminal. Or you could use os.popen2 or
subprocess.Popen (the newer, preferred, more complicated way) and get
both at once.
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?

For this example, where you just want to print stuff out, just use
subprocess.call(['ls','-l'])

For more complicated examples:

pr = subprocess.Popen(['ls','-l'],stdout=subprocess.PIPE)
file_list = pr.stdout.readlines()
 
B

banu

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()

So, here's what you're doing manually.
1) Open up a terminal, type "cd /etc". And then close that terminal
2) Open up a new terminal, type "ls -l" and wonder why it's not in /etc
Can anyone suggest me how to fix this simple script? Also what is the
use of read(), readlines() and write() functions?

The os and os.path modules contain higher-level functions than popen.
Such as os.listdir and os.chdir (if you really want to change the
current directory for the program).

popen returns a file object. In your case, because you opened it in
write mode, it's stdin so write will send things to the program
(assuming it reads from stdin), and read/readlines are useless. If you
were to open it in read mode, pr would be stdout and you would use
pr.read() or pr.readlines() to get your directory list instead of
having it print out to the terminal. Or you could use os.popen2 or
subprocess.Popen (the newer, preferred, more complicated way) and get
both at once.


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?

For this example, where you just want to print stuff out,  just use
subprocess.call(['ls','-l'])

For more complicated examples:

pr = subprocess.Popen(['ls','-l'],stdout=subprocess.PIPE)
file_list = pr.stdout.readlines()


Regards
Varun

Thanks for reply Benjamin.I got it now.
 
A

Albert Hopkins

Thanks for the reply Jon
Basically I need to move into a folder and then need to execute some
shell commands(make etc.) in that folder. I just gave 'ls' for the
sake of an example. The real problem I am facing is, how to stay in
the folder after popen('cd directory') finishes. It seems trivial, but
I am not able to do it.

The problem is that you are running 2 child, and it's the first
subprocess that's changing the directory and then exiting. This
actually has little to do with Python specifically. you can see the
same thing if you do this:

$ pwd
/tmp
$ cat mycd.sh
#!/bin/sh

cd /etc
$ ./mycd.sh
$ pwd
/tmp

As you can see I am still in tmp. This is because mycd.sh changed
to /etc/ but after it exits back to the parent process it is back
in /tmp.

What you want is to "cd" inside your script itself. os.chdir() does
this.
 
S

Sean DiZazzo

Thanks for the reply Jon
Basically I need to move into a folder and then need to execute some
shell commands(make etc.) in that folder. I just gave 'ls' for the
sake of an example. The real problem I am facing is, how to stay in
the folder after popen('cd directory') finishes. It seems trivial, but
I am not able to do it.

Varun

Use subprocess.Popen() with it's "cwd" argument. Something like:

import subprocess
p = subprocess.Popen(["ls","-l"] stdout=subprocess.PIPE, cwd="/etc")

print p.stdout.read()

~Sean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top