PDA

View Full Version : Python - Retrieving output from os.system()



Bodsda
August 31st, 2009, 06:59 PM
Hi guys,

I am having a bit of trouble retrieving information output from os.system() calls. To be specific, I am trying to do the following:

cwd = os.system("pwd")

I was hoping to get the current working directory string that the above command prints out, stored in cwd, instead I get the exit status from the 'pwd' command.

Is there any way to retrieve the data I need, through os.system() or any other type of system call.

Kind regards,
Bodsda

credobyte
August 31st, 2009, 07:03 PM
I'm far away from "I know Python", but .. answer could be found @ http://mail.python.org/pipermail/python-list/2005-October/343901.html ;)

Bodsda
August 31st, 2009, 07:06 PM
I'm far away from "I know Python", but .. answer could be found @ http://mail.python.org/pipermail/python-list/2005-October/343901.html ;)

Yeah, I tried using the subprocess module, specifically:

cwd = subprocess.call(["pwd"])
But I just got the exit status again.

Thanks,
Kind regards,
Bodsda

unutbu
August 31st, 2009, 07:19 PM
Hi Bodsda, how about this:


#!/usr/bin/env python
from subprocess import Popen,PIPE,STDOUT,call

proc=Popen('pwd', shell=True, stdout=PIPE, )
output=proc.communicate()[0]
print output


or, in this case,


import os
print(os.getcwd())


PS. The subprocess code I learned from Doug Hellmann's wonderful Python Module of the Week blog: http://blog.doughellmann.com/2007/07/pymotw-subprocess.html

Bodsda
August 31st, 2009, 07:20 PM
Hmm,

ouput = os.popen("pwd").readlines()
seems to get me a list of the output. Thanks for everyones help.

Kind regards,
Bodsda

Bodsda
August 31st, 2009, 07:21 PM
Hi Bodsda, how about this:


#!/usr/bin/env python
from subprocess import Popen,PIPE,STDOUT,call

proc=Popen('pwd', shell=True, stdout=PIPE, )
output=proc.communicate()[0]
print output


or, in this case,


import os
print(os.getcwd())


PS. The subprocess code I learned from Doug Hellmann's wonderful Python Module of the Week blog: http://blog.doughellmann.com/2007/07/pymotw-subprocess.html

Hi unutbu,

Could you explain your first code block please. I'm new to the popen attribute

Thanks,
Kind regards,
Bodsda

unutbu
August 31st, 2009, 07:31 PM
Explanation? Uh-oh...
Here is where I expose my lack of understanding: :)


proc=Popen('pwd', shell=True, stdout=PIPE)

This makes proc an instance of the Popen class.
This line, as I understand it, sets up a subshell which runs the 'pwd' command.
The stdout file handle is directed to PIPE. If you don't do this, then the subshell prints its output to the terminal. By directing it to the PIPE, you can then read what has been sent to the PIPE via the command


proc.communicate()

proc.communicate() returns a 2-tuple: (stdout, stderr)

So


output=proc.communicate()[0]

picks of the stdout, and ignores stderr.

Hope this helps. I'm sure Doug Hellmann explains it better.

PS. The official documentation on the subprocess module can be found here: http://docs.python.org/library/subprocess.html#module-subprocess

Bodsda
August 31st, 2009, 07:40 PM
Explanation? Uh-oh...
Here is where I expose my lack of understanding: :)


proc=Popen('pwd', shell=True, stdout=PIPE)

This makes proc an instance of the Popen class.
This line, as I understand it, sets up a subshell which runs the 'pwd' command.
The stdout file handle is directed to PIPE. If you don't do this, then the subshell prints its output to the terminal. By directing it to the PIPE, you can then read what has been sent to the PIPE via the command


proc.communicate()

proc.communicate() returns a 2-tuple: (stdout, stderr)

So


output=proc.communicate()[0]

picks of the stdout, and ignores stderr.

Hope this helps. I'm sure Doug Hellmann explains it better.

PS. The official documentation on the subprocess module can be found here: http://docs.python.org/library/subprocess.html#module-subprocess

Brilliant, thanks again unutbu,

Kind regards,
Bodsda

MadMan2k
August 31st, 2009, 08:25 PM
import commands
print commands.getoutput("pwd")

days_of_ruin
August 31st, 2009, 09:10 PM
import os
print os.getcwd()

Bodsda
August 31st, 2009, 09:16 PM
import os
print os.getcwd()

Thanks, this also works

However I was also after a more generic way of doing it for any command.

Thanks for all your help guys

Kind regards,
Bodsda

pepperphd
August 31st, 2009, 09:33 PM
I suppose this is a way to do it. Kind of cheap though.



os.system('ls -a &> _temp_')
out_file = open('_temp_', 'r')
out = out_file.read()
out_file.close()
os.remove('_temp_')
For your specific need:



os.system('pwd &> _temp_')
out_file = open('_temp_' r')
out = out_file.read()
out_file.close()
os.remove('_temp_')
print out


You might have to put the full path to _temp_ in os.remove(). I didn't test this.

spupy
September 1st, 2009, 12:24 AM
import commands
print commands.getoutput("pwd")


I don't know what you guys are talking about. This here is the way. It can't be simpler.
You can also get the return status along with the output. Check the documentation of the commands module.