I think the problem is with the statement for line in proc.stdout, which reads the entire input before iterating over it. The solution is to use readline() instead:
#filters output
import subprocess
proc = subprocess.Popen(['python','fake_utility.py'],stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline()
if not line:
break
#the real code does filtering here
print "test:", line.rstrip()
Of course you still have to deal with the subprocess' buffering.
Note: according to the documentation the solution with an iterator should be equivalent to using readline(), except for the read-ahead buffer, but (or exactly because of this) the proposed change did produce different results for me (Python 2.5 on Windows XP).
I think the problem is with the statement for line in proc.stdout, which reads the entire input before iterating over it. The solution is to use readline() instead:
#filters output
import subprocess
proc = subprocess.Popen(['python','fake_utility.py'],stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline()
if not line:
break
#the real code does filtering here
print "test:", line.rstrip()
Of course you still have to deal with the subprocess' buffering.
Note: according to the documentation the solution with an iterator should be equivalent to using readline(), except for the read-ahead buffer, but (or exactly because of this) the proposed change did produce different results for me (Python 2.5 on Windows XP).
Bit late to the party, but was surprised not to see what I think is the simplest solution here:
import io
import subprocess
proc = subprocess.Popen(["prog", "arg"], stdout=subprocess.PIPE)
for line in io.TextIOWrapper(proc.stdout, encoding="utf-8"): # or another encoding
# do something with line
(This requires Python 3.)
How to get output from subprocess.Popen along with visible execution?
Python subprocess output to stdout - Stack Overflow
subprocess.Popen and output
Capturing output from subprocess.run()
You’ll need to use the stdout and/or stderr arguments. https://docs.python.org/3/library/subprocess.html#subprocess.run
Edit: link to #subprocess.run
More on reddit.comVideos
Simply don't send the output to a pipe:
proc = subprocess.Popen (command_args, shell=False)
proc.communicate()
This work for me:
If need execute command in powershell :
import subprocess
S_V = subprocess.Popen(["powershell.exe", 'your command' ], stdout=subprocess.PIPE)
out, err = S_V.communicate()
print (out)
print (err )
Or if need execute command in CMD :
name=subprocess.check_output('whoami') # replace your command with 'whoami'
name=name.decode("utf-8")
print (name)
Note: whoami Displays user for the user who is currently logged on to the local system .
I'm trying to grok subprocess.Popen to run and monitor a command line executable in the background. (Specifically, the HandbrakeCLI video converter.)
In this I have been partially successful, using the following command:
handbrake = subprocess.Popen( cmd )
Where cmd is a list of parameters. When I do this, I can do other things while it's running, poll() it, and terminate it if desired, and unless I kill it, it runs to completion exactly the way I want. The problem is that I also want to suppress the output. No problem, right?
handbrake = subprocess.Popen( cmd, stdout=subprocess.PIPE )
This works, but there's still SOME output. Now, my first thought was that the messages I was seeing were techincally ERROR messages. So I tried two different methods:
handbrake = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE )handbrake = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
In both cases, the subprocess launches, I can do other things, but... it doesn't do anything. I can see HandbrakeCLI in the task manager, but it's not using any resources (where it *should* be using nearly 100%), and no file has been created in the target directory.
This leaves me with two questions that may or may not be related:
-
Why is redirecting stdout causing the program to do nothing?
-
Where is that other output coming from, and how do I suppress it? (Helpfully, it doesn't contain any information I need to capture.)
Lets say I run:
subprocess.run(["wmctrl", "-l"])
This is great as it lists all open windows! However I can't figure out how to put this output into a variable of some kind so I can do something with it. Any help is appreciated.
You’ll need to use the stdout and/or stderr arguments. https://docs.python.org/3/library/subprocess.html#subprocess.run
Edit: link to #subprocess.run
Thanks for the answers. In the mean time I did get this to work...
import subprocess
output = subprocess.check_output("wmctrl -l", shell=True)
# it returns a byte type so convert it to string
output = str(output)
# split it into a list that makes sense
output = output.split('\\n')
# print out our list of windows
for x in range(len(output)):
print (output[x])
Thanks again! Just thought I'd share what worked for me for other beginners.