This question already has answers here:
Pipe subprocess standard output to a variable [duplicate]
(3 answers)
Python subprocess Popen stdout to variable only [duplicate]
(1 answer)
Redirect subprocess to a variable as a string [duplicate]
(2 answers)
Closed 4 years ago.
I'd like to retrieve the output from a shell command
In [7]: subprocess.Popen("yum list installed", shell=True)
Out[7]: <subprocess.Popen at 0x7f47bcbf6668>
In [8]: Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Installed Packages
GeoIP.x86_64 1.5.0-11.el7 #anaconda
NetworkManager.x86_64
....
The results are output to the console,
How could I hold the output to a variable saying "installed_tools"?
Try setting stdout and/or stderr to subprocess.PIPE.
import subprocess as sp
proc = sp.Popen("yum list installed", shell=True, stdout=sp.PIPE, stderr=sp.PIPE)
out = proc.stdout.read().decode('utf-8')
print(out)
As suggested in comments, it's better to use Popen.communicate() in case stderr needs reading and gets blocked. (Thanks UtahJarhead)
import subprocess as sp
cp = sp.Popen("yum list installed", shell=True, stdout=sp.PIPE, stderr=sp.PIPE).communicate()
out = cp[0].decode('utf-8')
print(out)
Related
This question already has answers here:
How do I write to a Python subprocess' stdin?
(5 answers)
How do I pass a string into subprocess.Popen (using the stdin argument)?
(12 answers)
Write to a Python subprocess's stdin without communicate()'s blocking behavior
(1 answer)
How to use `subprocess` command with pipes
(7 answers)
Closed 3 years ago.
I am using Python and wants to run the "editUtility" as shown below.
echo "Some data" | /opt/editUtility --append="configuration" --user=userid 1483485
Where 1483485 is some random number and passed as parameter too.
What I am doing is calling the "editUtility" via Python "subprocess" and passing the param as shown below.
proc = subprocess.Popen(['/opt/editUtility', '--append=configuration'],stdout=subprocess.PIPE)
lsOutput=""
while True:
line = proc.stdout.readline()
lsOutput += line.decode()
if not line:
break
print(lsOutput)
My question is: How to pass all of the params mentioned above and how to fit the 'echo "some data"' along with pipe sign with subprocess invocation?
so if you just want to input a string and then read the output of the process until the end Popen.communicate can be used:
cmd = [
'/opt/editUtility',
'--append=configuration',
'--user=userid',
'1483485'
]
proc = subprocess.Popen(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
(stdoutData, stderrData) = proc.communicate('Some data')
This question already has an answer here:
Opening a process with Popen and getting the PID
(1 answer)
Closed 3 years ago.
I want to use python to execute shell commands, how to get the process ID of the process generated by the shell command.
import subprocess
subprocess.Popen(['{}'.format(self.executor), '-c', {}'.format(config), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
The process executed by this shell command does not exit by itself, I need to run 10 min to KILL it, how can I get the id of the process?
You have to store the object Popen returns then use its pid attribute:
p = subprocess.Popen(...)
print(p.pid)
This question already has answers here:
How to use `subprocess` command with pipes
(7 answers)
Closed 5 years ago.
I write this command on linux
netstat -ant | wc -l
but when I try to call from python with
subprocess.Popen(['netstat','-ant','|','wc','-l'])
I cant get all output, I see just result of first command (netstat -ant).
How can I process this command on python ? (note: this command gives a int as a result)
I don't know if there's any easier method but you can go like:
from subprocess import run, Popen, PIPE
sess1 = run(['netstat', 'ant'], stdout=PIPE)
sess2 = Popen(['grep', '"SYN"'], stdin=PIPE)
sess2.stdin.write(sess1.stdout)
sess2.communicate() # required?
This question already has answers here:
Python subprocess Popen: Why does "ls *.txt" not work? [duplicate]
(2 answers)
Closed 6 years ago.
>>> import subprocess
>>> child = subprocess.Popen(["ls", "examples/*"], stdout=subprocess.PIPE)
>>> ls: examples/*: No such file or directory
But from terminal it works
Beagle:kumarshubham$ ls examples/*
examples/convert_greyscale.py examples/feat_det_harris_corner.py examples/read_display_image.py
examples/example_set_roi.py examples/manipulate_img_matplotlib.py examples/remove_matplotlib_cache.py
Can one guide me where i am going wrong?
import subprocess
child = subprocess.Popen(["cd /to-your-PATH/; ls", "examples/*"],shell=True, stdout=subprocess.PIPE)
child.stdout.read()
This is because of (*) wild card usage. You need to supply shell=True to execute the command through shell interpreter
>>> import subprocess
>>> child = subprocess.Popen(["ls", "examples/*"], stdout=subprocess.PIPE, shell=True)
You should add shell=True in your Popen call even if * is useless in your case, ls examples/ should return the same output:
child = subprocess.Popen(["ls", "examples/*"], stdout=subprocess.PIPE, shell=True)
Plus, more pythonic approach could be:
import os
os.listdir('examples')
you can use additional parameter shell=True, then it would look:
child = subprocess.Popen(["ls", "examples/*"], shell=True, stdout=subprocess.PIPE)
NB! even this will work, - according to official python documentation https://docs.python.org/2/library/subprocess.html using shell=True might be a security issue.
This question already has answers here:
Store output of subprocess.Popen call in a string [duplicate]
(15 answers)
Closed 8 years ago.
I need to capture the stdout of a process I execute via subprocess into a string to then put it inside a TextCtrl of a wx application I'm creating. How do I do that?
EDIT: I'd also like to know how to determine when a process terminates
From the subprocess documentation:
from subprocess import *
output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0]
Take a look at the subprocess module.
http://docs.python.org/library/subprocess.html
It allows you to do a lot of the same input and output redirection that you can do in the shell.
If you're trying to redirect the stdout of the currently executing script, that's just a matter of getting a hold of the correct file handle. Off the top of my head, stdin is 0, stdout is 1, and stderr is 2, but double check. I could be wrong on that point.