# Run a C# application from python script

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!

• Just curious, but why do the rest in Python (and not in C#)? Jul 15 '10 at 21:10
• If you're looking for unit tests, try NUnit: nunit.org Jul 15 '10 at 21:11
• @Henk: I wanted to avoid making changes to the model itself. Thus, doing what I want requires writing a new app/script. The performance of the solution is not important, but the time taken to code it is. I thought a scripting language would be better for a scripting sort of problem. Plus, I like python more. :) Jul 15 '10 at 21:21
• A bit off topic, but maybe something that might interest you. Have you thought of using IronPython? It allows you to run python scripts from C# (some exceptions related to C libraries). Also you can work with the .Net framework using IronPython. Just a thought. Jul 15 '10 at 21:42

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))

• Ah-ha someone that actually knows python instead of just looking at docs like me... touche, this should be marked correct. Jul 15 '10 at 21:15
• An improvement would be using os.rename instead of execing out to move. Jul 16 '10 at 1:50
• note: subprocess is available since Python 2.4. Popen doesn't wait for the program to finish -- it just starts it i.e., move command is run too soon unless app.exe is very quick. You could use subprocess.check_call to wait for the child process to end
– jfs
May 5 '14 at 9:11
• A question: Can I have my .exe file developed w/ C# on a linux server and run it directly through a python script like this, or do I need to go through something like Mono? Sep 24 '15 at 7:47
• @user2875994 All this is doing is executing the exe file using the shell. So if you've set up your server with something like mono such that executing the file with your command terminal will run it, then it'll work. Otherwise you may have to use cmdLine = r"mono c:\path\to\my\app.exe" and have mono installed.
– Aren
Sep 24 '15 at 16:50

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.

• Ah, exactly what I needed. I can't believe I didn't see that earlier. Thank you very much! Jul 15 '10 at 21:07
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.15417566895484924, "perplexity": 1446.0414537220686}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-43/segments/1634323585380.70/warc/CC-MAIN-20211021005314-20211021035314-00249.warc.gz"}