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.
Related
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'])
This question already has answers here:
Nohup is not writing log to output file
(7 answers)
Python script writes no output when stdout is redirected to a file [duplicate]
(1 answer)
Using tee to get realtime print statements from python [duplicate]
(1 answer)
Python app does not print anything when running detached in docker
(13 answers)
Closed 2 years ago.
I have a Python script like the following:
#!/usr/bin/env python
print("Start")
sql = get_sql_from_file(...)
# The following function takes a long time to run
execute_oracle_driver(sql)
print("Finished")
I execute it with:
(my-env) $ time nohup python script.py &
Then I check if there's any initial output, but nothing:
(my-env) $ cat nohup.out
(my-env) $
I would at least expect to see "Start" after no more than a few seconds. Even after waiting a few minutes, then killing it, I get no output. If possible, how can I fix this?
Output is buffered by default, so you would need to ensure that the output is flushed; otherwise, no output will be seen in nohup.out until the end of the program. One way to do that is as follows:
print("Start", flush=True)
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
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.
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.