I want the output of the jobs command run in the shell as a string in python.
I have this code
import subprocess
p1 = subprocess.Popen(['jobs'], shell=True, stdout=subprocess.PIPE)
print p1.communicate()
But this doesnt seem to work. The output I get is -
('', None)
How do I fix this?
asked Sep 19, 2012 at 9:52
ssb
7,51211 gold badges40 silver badges63 bronze badges
1 Answer 1
You can use subprocess.check_output:
In [5]: import subprocess
In [6]: output = subprocess.check_output("ps")
In [7]: print output
PID TTY TIME CMD
2314 pts/2 00:00:06 bash
4084 pts/2 00:00:03 mpdas
7315 pts/2 00:00:02 python
7399 pts/2 00:00:00 ps
In [8]:
Your code works fine for me.
In [11]: import subprocess
In [12]: p1 = subprocess.Popen(['ps'], stdout=subprocess.PIPE)
In [13]: print p1.communicate()[0]
PID TTY TIME CMD
2314 pts/2 00:00:06 bash
4084 pts/2 00:00:03 mpdas
7315 pts/2 00:00:02 python
7682 pts/2 00:00:00 ps
In [14]:
answered Sep 19, 2012 at 10:00
RanRag
49.8k39 gold badges120 silver badges172 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py
p1.returncode? (Btw., you don't needshell=True.)