1

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

TTT
  • 4,354
  • 13
  • 73
  • 123
  • A similar question was discussed before. Hope [this](http://stackoverflow.com/questions/1811691/running-an-outside-program-executable-in-python) helps. – AurA Apr 30 '12 at 05:06
  • what do you mean by output file? are you trying to create the output files where your py script exist or where the dosefile.exe exist – anselm Apr 30 '12 at 05:07
  • Maybe he wants to pipe the STDOUT / STDERR output. – ninMonkey Apr 30 '12 at 05:12
  • @anselm: the output file was created by the dosefile.exe automatically. So I assume that if I can call this DOS exe from python, I should have those generated output files. – TTT Apr 30 '12 at 05:13
  • 1
    `os.system("I:/dosefile.exe")` should work. copy your py script to `I:/` and run `os.system("dosefile.exe")` – anselm Apr 30 '12 at 05:17
  • @anselm: thanks for your suggestion. 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? – TTT Apr 30 '12 at 05:22

2 Answers2

1

Try calling it as a system call

import os
os.system('I:/dosefile.exe')

Then parse the output files it creates.

TheRealKingK
  • 829
  • 7
  • 14
  • Thanks for the suggestion. I tried this approach. But my return value is 8. Does that mean the dos file was not properly called? – TTT Apr 30 '12 at 05:17
  • It worked after putting .py script to the folder with dosefile.exe. Thanks! – TTT Apr 30 '12 at 05:24
1

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.

oz123
  • 27,559
  • 27
  • 125
  • 187
  • Thanks for recommending the subprocess approach. I am using it. However, my .exe file will generate a bunch of files and save them to my hard drive. Is there a way to use stdout to pass them to Python? – TTT Apr 30 '12 at 17:42
  • do you know the names of the files created ? if these are text files, it should be quite straight forward to read them when Popen.communicate() is done. – oz123 Apr 30 '12 at 17:47
  • Yes, the output files can be opened as a text file and I know their names. However, I just have over 100 of them and the number can be increased based on the duration of simulation. – TTT Apr 30 '12 at 19:14
  • if everytime you make new simulation, the files are created in the same directory os.listdir() is your friend. (do you now IPython? if not... do get to know... it will help you learn Python faster) – oz123 Apr 30 '12 at 20:33