Reputation: 7502
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?
Upvotes: 0
Views: 403
Reputation: 49567
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]:
Upvotes: 1