views:

44

answers:

2

I've just about finished coding a decently sized disease transmission model in C#. However, I'm fairly new to .NET and am unsure how to proceed. Currently I just double-click on the .exe file and the model imports config setting from text files, does its thing, and outputs the results into a text file.

What I would like to do next is write a Python script to do the following:

  • Run the simulation N times (N > 1000)
  • After each run rename the output file and store (i.e. ./output.txt -> ./acc/outputN.txt)
  • Aggregate, parse, and analyze the outputs
  • Output the result in some clean format (possibly excel)

The majority of my programming experience to date has been in C/C++ on linux. I'm fairly confident about the last two items; however, I have no idea how to proceed for the first two. Here are some specific questions I'd like advice on:

  • What is the easiest/best way to run my C# .exe from a python script?
  • Does anyone have advice on the best way to do filesystem operations in Python on a Windows system?

Thanks!

+3  A: 

The answer to your problems can be found in 'os' in the python standard library. Documentation for doing various operations, such as handling files and starting processes, can be found here.

Process management (Running your C# program) can be found here and file operations are here.

EDIT: Actually, instead of the above process link, you should use the subprocess module.

kersny
Ah, exactly what I needed. I can't believe I didn't see that earlier. Thank you very much!
Mandelbrot
+4  A: 

As of Python 2.6+ you should be using the subprocess module: (Docs)

import subprocess

for v in range(1000):
    cmdLine = r"c:\path\to\my\app.exe"
    subprocess.Popen(subprocess)
    subprocess.Popen(r"move output.txt ./acc/output-%d.txt" % (v))
Aren
Ah-ha someone that actually knows python instead of just looking at docs like me... touche, this should be marked correct.
kersny
Perfect, thanks a lot!
Mandelbrot
An improvement would be using `os.rename` instead of execing out to `move`.
Jed Smith