如何在c++中使用system()来运行包含空格的python脚本

How to use system() in c++ to run python script containing space

本文关键字:包含 运行 空格 脚本 python c++ system      更新时间:2023-10-16

我希望在c++代码中运行python脚本,但地址路径包含空格
以下是我要做的:

string python = ""D:\my folder\python.exe"";
string script = ""D:\my scripts\hello.py"";
string arg = ""argument 1"";
string cmd= python + " " + script + " " + arg;
system(cmd.c_str());

这里cmd""D:\my folder\python.exe" "D:\my scripts\hello.py"" ""argument 1""
,它是文本形式的,期望被执行为
"D: \my folder\python.exe"quot;D: \my scripts\hello.py"quot;自变量1";作为命令行,但执行失败,并出现错误->'D: "\my"未被识别为内部或外部命令。

但同样的命令";D: \my folder\python.exe"quot;D: \my scripts\hello.py"在命令提示符下工作正常吗
请在c++中建议如何执行路径中有空格的python脚本,并向脚本传递参数。

好吧,我找到了一种方法让它与系统((一起工作我试着和执行官谈过,但还是有一些问题。需要用双引号覆盖整个命令。

在我的情况下,它应该是"""D:\my folder\python.exe" "D:\my scripts\hello.py"" ""argument 1"""