从python脚本运行一个C#应用程序 [英] Run a C# application from python script

查看:201
本文介绍了从python脚本运行一个C#应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚完成了用C#编写了一个体积适中的疾病传播模型。不过,我对.NET相当陌生,不确定如何继续。目前我只需双击.exe文件,模型从文本文件导入配置设置,做它的事情,并输出结果到一个文本文件。



接下来我要做的是编写一个Python脚本来完成以下工作:


  • 运行模拟N次(N> 1000)
  • 每次运行后重命名输出文件并存储(即./output.txt - > ./acc/outputN.txt )

  • 汇总,分析和分析输出结果
  • 以一种干净的格式输出结果(可能是excel)


到目前为止,我的大部分编程经验都是在Linux上的C / C ++中进行的。对于最后两项我相当有信心。然而,我不知道如何进行前两个。这里有一些具体的问题,我想建议:


  • 什么是从python运行我的C#.exe最简单/最好的方法脚本?

  • 是否有人对在Windows系统上执行Python文件系统操作的最佳方式有任何建议?


谢谢!

解决方案

从Python 2.6开始,应该使用子进程 module:(文档

  import subprocess 

for v in range(1000):
cmdLine = rc:\ path \ to\my\app.exe
subprocess.Popen(subprocess)
subprocess.Popen(rmove output.txt ./acc/output-%d.txt%( v))


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!

解决方案

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

这篇关于从python脚本运行一个C#应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆