簡體   English   中英

如何在 c++ 中使用 system() 來運行包含空格的 python 腳本

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

我希望在 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" "D:\my scripts\hello.py" "argument 1" 作為命令行,但這無法執行並出現錯誤 ->'D:\my' 未被識別為內部或外部命令。

但是相同的命令 "D:\my folder\python.exe" "D:\my scripts\hello.py" 在命令提示符下工作正常嗎?
請在 c++ 中建議如何執行在路徑中有空格的 python 腳本並將參數傳遞給腳本。

好吧,我找到了一種方法讓它與我嘗試使用 exec 的 system() 一起工作,但那里仍然存在一些問題。 需要用雙引號覆蓋整個命令。

在我的情況下,它應該是"\"\"D:\\my folder\\python.exe\" \"D:\\my scripts\\hello.py\"" "\"argument 1\"\""

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM