Running an outside program (executable) in Python?
Those whitespaces can really be a bother. Try os.chdir('C:/Documents\ and\ Settings/')
followed by relative paths for os.system
, subprocess
methods, or whatever...
If best-effort attempts to bypass the whitespaces-in-path hurdle keep failing, then my next best suggestion is to avoid having blanks in your crucial paths. Couldn't you make a blanks-less directory, copy the crucial .exe
file there, and try that? Are those havoc-wrecking space absolutely essential to your well-being...?
Running an external executable in Python interactively
In addition to the errors in your C program that have been mentioned, you are calling .communicate()
more than once. .communicate()
is single-use function that sends all of its data to the subprocess and waits for the subprocess to exit.
If you must use .communicate()
, use it this way:
com = ris.communicate(input='bye\nq\n')
Alternatively, you can use ris.stdin.write()
, like so:
import subprocess
from subprocess import PIPE, STDOUT
output = open("test.out","w")
ris = subprocess.Popen(executable="/tmp/test.exe", args="", stdin=PIPE,
stdout=output, stderr=STDOUT,
universal_newlines=True, shell=True)
com = ris.stdin.write('bye\n')
com = ris.stdin.write('q\n')
ris.stdin.flush()
output.close()
ris.wait()
How to run exe file from python?
Problem solved.
Thanks everyone, the issue was administrative privileges.. Started pycharm as administrator.
And like i said - i was able to see the file with os.listdir(), but when i try to run it, errors start to pop up.
I think the main issue is os.system() inherit current privileges from python proccess.
How do I execute a program or call a system command?
Use the subprocess
module in the standard library:
import subprocess
subprocess.run(["ls", "-l"])
The advantage of subprocess.run
over os.system
is that it is more flexible (you can get the stdout
, stderr
, the "real" status code, better error handling, etc...).
Even the documentation for os.system
recommends using subprocess
instead:
The
subprocess
module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in thesubprocess
documentation for some helpful recipes.
On Python 3.4 and earlier, use subprocess.call
instead of .run
:
subprocess.call(["ls", "-l"])
Opening external program in python
I've found a solution for my case, I think there is a problem with Flask upload module, my C++ program was on the same path as my uploaded files from flask, so when I changed the program path and used Windows command prompt to start the program, everything worked like a charm.
Related Topics
Importerror: Numpy.Core.Multiarray Failed to Import
SQL Join or R's Merge() Function in Numpy
Python Code to Remove HTML Tags from a String
Pandas Read_HTML Valueerror: No Tables Found
Why Is Variable1 += Variable2 Much Faster Than Variable1 = Variable1 + Variable2
Beautifulsoup Not Grabbing Dynamic Content
Opencv Videocapture and Error: (-215:Assertion Failed) !_Src.Empty() in Function 'Cv::Cvtcolor'
How to Plot and Annotate a Grouped Bar Chart
Logger Configuration to Log to File and Print to Stdout
Difference in Boto3 Between Resource, Client, and Session
What Are Some Good Python Orm Solutions
Remove Xticks in a Matplotlib Plot
How to Convert SQL Query Result to Pandas Data Structure
Pandas Groupby Range of Values
How to Refer to Relative Paths of Resources When Working with a Code Repository