【发布时间】:2021-08-19 09:47:13
【问题描述】:
我使用 Visual Studio 创建了以下 C# 控制台应用程序 (.NET Core 3.1):
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
//Check if args contains anything
if (args.Length > 0)
{
Console.WriteLine("args = " + args[0]);
} else
{
Console.WriteLine("args is empty");
}
//Prevent the application from closing itself without user input
string waiter = Console.ReadLine();
}
}
}
我可以通过 cmd.exe 使用一个参数成功执行应用程序: CMD Input and Output
现在我想通过 Python 使用一个参数来运行这个程序。我已阅读 How to execute a program or call a system command? 并尝试实现它。但是,应用程序似乎没有正常运行。
我通过 Jupyter notebook 使用的 python 代码:
import subprocess
path = "C:/Users/duykh/source/repos/ConsoleApp2/ConsoleApp2/bin/Release/netcoreapp3.1/ConsoleApp2.exe"
subprocess.Popen(path) //#Also tried with .call and .run
//#subprocess.Popen([path, "argumentexample"]) doesn't work either
输出:
<subprocess.Popen at 0x2bcaeba49a0>
我的问题是:
为什么它没有(正确)运行,我如何用一个参数正确运行这个应用程序?
【问题讨论】:
-
也许另一种选择是从 Python 调用/运行 shell 或 cmd 并从那里运行应用程序?
标签: python c# console-application