从 python 脚本运行 C# 应用程序

发布于 2024-09-10 13:17:52 字数 520 浏览 3 评论 0原文

我刚刚用 C# 编写了一个相当大的疾病传播模型。但是,我对 .NET 相当陌生,不确定如何继续。目前,我只需双击 .exe 文件,模型就会从文本文件导入配置设置,执行其操作,并将结果输出到文本文件中。

接下来我想做的是编写一个 Python 脚本来执行以下操作:

  • 运行模拟 N 次(N > 1000)
  • 每次运行后重命名输出文件并存储(即 ./output.txt -> ./acc /outputN.txt)
  • 聚合、解析和分析输出
  • 以某种干净的格式输出结果(可能是 Excel)

迄今为止,我的大部分编程经验都是在 Linux 上使用 C/C++ 进行的。我对最后两项相当有信心;但是,我不知道如何继续前两个。以下是我想要咨询的一些具体问题:

  • 从 python 脚本运行 C# .exe 的最简单/最佳方法是什么?
  • 有人对在 Windows 系统上用 Python 进行文件系统操作的最佳方法有建议吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽你眉间 2024-09-17 13:17:52

从 Python 2.6+ 开始,您应该使用 subprocess 模块:(文档)

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

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))
蓝海 2024-09-17 13:17:52

您的问题的答案可以在 python 标准库中的“os”中找到。有关执行各种操作(例如处理文件和启动进程)的文档,可以在此处找到。

进程管理(运行 C# 程序)可以在此处找到并文件操作在此处

编辑:实际上,您应该使用 subprocess 模块

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文