Sorry if this question has been asked many times. I have a DOS .exe file. It works well by double clicking it in Windows, which generates some output files in the working folder. However, I need to call it from Python. I have tried the following commands (subprocess.Popen and os.system), but it only opens a DOS window without saving output files. So can someone give me a hint?
Thanks!
CODE
a=subprocess.Popen(r"I:/dosefile.exe")
a1=os.system(r"I:/dosefile.exe")
UPDATE It worked by putting the .py script file to the fold containing dosefile.exe. Is there a way to make it work without migrating .py file?
Figure it out. I need to change the working folder by using os.chdir() if .py file is somewhere else
2 Answers 2
Try calling it as a system call
import os
os.system('I:/dosefile.exe')
Then parse the output files it creates.
A better thing would be to use:
os.chdir('I:')
and then you could use subrprocess.
os.system is very limited and subprocess.Popen should be preferred.
Even better would be to use the subprocess.Popen keyword 'cwd', which sets the working directory.
subprocess.Popen(self, args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None,\
preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, \
startupinfo=None, creationflags)
Python is an easy language to practice, and stuff like os.system does work, because Python is very forgiving (not like C...). However, I really recommend to make an effort to learn the more powerful tools that come with Python.
You won't be sorry later on.
os.system("I:/dosefile.exe")should work. copy your py script toI:/and runos.system("dosefile.exe")