Actually I create ".exe" file using C++ that takes 1 value as input and calculate the result. Now i want to pass the value form a python program to the ".exe" directly. Is is actually possible??
One thing you can do is output the python program to a file. From the console it looks like:
$ python script.py > output
or you can do it in Python by redirecting stdout to file:
import sys
sys.stdout = open('output', 'w')
Then you can use the output file as input to your exe.
$ program.exe output
Related
I'm trying to run a python file from within my python script. I'm doubtful that the file is even getting run in the first place and it is not showing anything in the stdout either for me to debug.
I have tried the command 'ls' in subprocess and it worked, and was in the proper directory that the temp.py file i am trying to run is.
When i have tried to set the argument 'shell=True' it takes me into the python repl for some reason i am not sure why.
Here is the string output:
Terminal output: CompletedProcess(args=['python3', 'temp.py'], returncode=0, stdout=b'')
And here's the code used to produce it:
result = subprocess.run(['python3', 'temp.py'], stdout=subprocess.PIPE, check=True)
print('Terminal output: '+str(result))
EDIT
I also just tried
process = Popen(['python3', 'temp.py'], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
print('Terminal output: '+str(stdout)+str(stderr))
No cigar:
Terminal output: b''b''
So, I found an alternative that worked. I dumped all the contents of the file into the python3 command with the '-c' augment.
process = Popen(['python3','-u', '-c', strCode], stdout=PIPE, stderr=PIPE)
strCode being the contents of the file.
I am writing a script to open a new command prompt, and run an executable. I got a working code using os.system. Can you help me transform it to subprocess.Popen?
hello_exe = r'c:\hello.exe'
os.system("start cmd /k {}".format(hello_exe))
I cannot run hello_exe as a background process because I want the user to see the command prompt and be able to scroll through the logs. Any help is appreciated. Thanks.
My environment: Windows8 and python 2.7.12
Conversion to subprocess.Popen is like this:
import subprocess
process = subprocess.Popen(r'c:\hello.exe', shell = True)
process.communicate()
Conversion to Popen
from subprocess import Popen, PIPE
hello_exe = r'c:\hello.exe'
proc = Popen(hello_exe, stdout=PIPE, shell = True)
proc.communicate()
Here are few links which you can refer to understand subprocess.Popen
Link to understand Popen
Another link to understand Popen
Link to 50+ examples of Popen
I encountered this problem after following a python tutorial on Youtube about creating text files. The instructor had us type in the following code to start:
def createFile(dest):
print dest
if__name__ == '__main__':
createFile('ham')
raw_input('done!')
We had created a folder on the desktop to store the file 'ham' in. But when I double clicked on 'ham' the command prompt window popped on then in a flash it popped off. I am an obvious beginner and I don't know what is going on. Can anyone explain this to me?
You can open command prompt then navigate to python interpreter
directory and run your program by typing python /diretory/to/your/program.py for
example if you have a program named test.py in the directory c:/python and you want to
run it and you have python interpreter installed in C:/python2.x/ directory
then you should open command prompt and type in
cd c:\python2.x\
then you should type
python c:/python/test.py
and perfectly it will work
showing you your program output .
I have a Fortran exe that is being moved on to an IaaS. The Fortan exe runs fine when run from console (Windows), but when I call the exe from Bottle RESTserver (locally) the file paths do not work and the I get the following error:
At line 79 of file MainCalculator.f90 (unit = 61, file = '└')
Fortran runtime error: File '' does not exist
Here is my Python / Bottle code to execute the Fortran:
def model():
curr_dir = os.path.dirname(os.path.realpath(__file__))
exe = "Calculator.exe"
path = os.path.join(curr_dir, 'bin', 'fortan_model', 'Debug', exe)
a = subprocess.Popen(path, shell=False)
a.wait()
Here is the line of code that throws the error in Fortran:
open(UNIT=61, FILE=trim(adjustl(recipePath))//"Scenarios.txt")
where, recipePath = '..\..\ourRecipes\OUR_recipes_082014\' (defined in another .f90 file)
It seems the file path is being reported as an ASCII value (file = '└'), which does not happen when the exe is run from command line. I thought it maybe the relative paths, but it gives the same error with absolute paths, but with file = '≡f*☺└'.
It also creates a file name ' Scenarios.txt' (with spaces in front).
EDIT: The Fortran .exe reads an input txt file. Each row is assigned to a variable. This works as intended from the command line, but when executed from Bottle, it parses the input file as blank characters. Do I need to grant Python permission to read the file and/or open the input file in memory?
I had a similar problem.
I solved this problem by decreasing the size of the output file name.
I am trying to pass arguments to an executable written in C from my script, however, the program is not executed and no console window appears which appears when the executable is run.
I have tried to run the executable using os.system() and subprocess.call(), both return 0 which I think means the command executed successfully but my executable doesn't run.
What am I missing here?
subprocess.call(["C:\Program Files (x86)\Hello\myApp.exe", "-i abc.txt -o xyz.pdf"],shell=True)
os.system('"'+'C:\Program Files (x86)\Hello\myApp.exe -i abc.txt -o xyz.pdf'+'"');
I have tried running without arguments as well still the program doesn't execute.
Unless myApp.exe is a shell command such as dir; don't use shell=True on Windows.
If you use a list argument then each list item should be a separate argument for your program:
from subprocess import check_call
check_call([r'C:\Program Files (x86)\Hello\myApp.exe', #NOTE: `r''` literal
'-i', 'abc.txt', '-o', 'xyz.pdf'])