How to get stdout into a string (Python) [duplicate] - python

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.

Related

How to run external program after python program executes [duplicate]

This question already has answers here:
Launch a totally independent process from Python
(2 answers)
Closed 1 year ago.
When i try to run something like
os.system('start')
or
os.system('start file.txt')
or
os.startfile('file.txt')
it does start console, notepad or whatever, but when my python program executes, those opened programs close as well.
So my question is: Is there a way to do things like above, but without closing those programs along with my script?
It will work with frunction from the subprocess module.
Here is an example with Popen :
from subprocess import Popen
p = Popen(['notepad.exe'])
You can also use call
import subprocess
subprocess.call(['calc.exe'])

use subprocess.Popen with an interactive program [duplicate]

This question already has an answer here:
Python subprocess and user interaction
(1 answer)
Closed 2 years ago.
I have a program that when I run in shell, it asks for input from the user.
When I run it as follows with subprocess.Popen, it does not get into the part of the program
where it asks for input and it just finishes. How could I change that?
cmd = ["./pairwise", "-seq", sites, "-loc", locs, "-lk", "species_genotypenew_lk.txt", "-prefix", ldhat_out]
subprocess.Popen(cmd)
Thanks!
if pairwise is reads from stdin and writes to stdout
subprocess.Popen(cmd).communicate()
should do it.

subprocess.Popen not printing/running properly [duplicate]

This question already has answers here:
Read streaming input from subprocess.communicate()
(7 answers)
Closed 7 years ago.
I have a script that reads from external sensors (and runs forever), when I run it as ./zwmeter /dev/ttyUSB0 300 it behaves normally and prints output continuously to stdout. I am using bash on Ubuntu. I'd like to execute this command as part of a python script. I have tried:
from subprocess import Popen, PIPE
proc = Popen(['./zwmeter', '/dev/ttyUSB0', '300'], stderr=PIPE, stdout=PIPE)
print proc.communicate()
but I get a program that runs forever without producing any output. I don't care about stderr, only stdout and have tried splitting up the printing but still with no success.
Thank you for any help you can provide!
I think the problem has to do with the process I'm calling not terminating. I found a good work around on this site:
http://eyalarubas.com/python-subproc-nonblock.html

Using greater than operator with subprocess.Popen [duplicate]

This question already has answers here:
Using greater than operator with subprocess.call
(2 answers)
Closed 7 years ago.
I want to redirect the output of python script to the file using greater than operator. I have below code which is not working properly. Can someone please help me on this?
proc= subprocess.Popen(['python', 'countmapper.py',file],cwd="C:\pythonPrograms\\",stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
countReducer= subprocess.Popen(['python', 'countreducer.py'],cwd="C:\pythonPrograms\\",stdout=subprocess.PIPE,stdin=proc.stdout, stderr=subprocess.STDOUT)
countpostprocesser= subprocess.Popen(['python','countpostprocesser.py','>','output.json'],cwd="C:\pythonPrograms\\",stdout=subprocess.PIPE,stdin=countReducer.stdout,stderr=subprocess.STDOUT)
'file' is name of the log file that I want to process. Last line (starting with countpostprocesser...) is failing.
Your call is failing because the redirection operator is being passed to your script as an argument, not being acted on by a shell to redirect your output to a file. See the Popen documentation.
This answer to another SO question shows a good example of opening a file, then redirecting the subprocess' output to the file.
Also, as shx2 mentioned in another answer, passing the shell=True argument to your Popen constructor should accomplish what you're looking for as well. It will cause the process to be opened in it's own shell, allowing the shell program to interpret the arguments you pass. Note an important line in the Popen documentation though: "If shell is True, it is recommended to pass args as a string rather than as a sequence."
Use the shell=True flag of Popen.
Also, as I mentioned in the comments, your task can be done simply and elegantly using plumbum.

Run programmes in a sequence using python [duplicate]

This question already has answers here:
Why is subprocess.Popen not waiting until the child process terminates?
(3 answers)
Closed 8 years ago.
I have a python programme as below
import os
import subprocess
for m in range(0,10):
os.chdir("C:/")
run="my command%d"%m
subprocess.Popen(run).wait()
Where 'my command' is something I used to launch another programme.
Although I have wait() after Popen, it turns out that the 10 programmes still run simultaneously, not as expected.
How do I settle this issue?
Two options:
use subprocess.check_call() (which should run sequentially)
use Popen.communicate() ( https://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate) with stdout and stderr set to subprocess.PIPE to see if stdout and stderr are indeed sequentially generated
Also, datetime.datetime.now() gives you a microsecond, so you can see time with granularity higher than 1s.

Categories