[英]Executing command line from python that contains spaces
I try to automatise an image stitching process from python using the software PTGui.我尝试使用软件 PTGui 自动执行来自 python 的图像拼接过程。 I can execute the following command which works perfectly in the windows command line:我可以执行以下在 windows 命令行中完美运行的命令:
C:\Users\mw4168> "C:\Program Files\PTGui\ptgui.exe" -batch "C:\Users\mw4168\OneDrive\Desktop\PTGui Tests\3 rows\Panorama.pts" command screenshot here C:\Users\mw4168> "C:\Program Files\PTGui\ptgui.exe" -batch "C:\Users\mw4168\OneDrive\Desktop\PTGui Tests\3 rows\Panorama.pts"命令截图在这里
However, when I try to execute this command using os.system or subprocess.run in Python:但是,当我尝试在 Python 中使用 os.system 或 subprocess.run 执行此命令时:
import os
os.system("C:\Program Files\PTGui\ptgui.exe" + "-batch" +"C:\Users\mw4168\OneDrive\Desktop\PTGui Tests\3 rows\panorama.pts")
I get this error:我收到此错误:
'C:\Program' is not recognized as an internal or external command, operable program or batch file. 'C:\Program' 不是内部或外部命令,也不是可运行的程序或批处理文件。 error screenshot here错误截图在这里
It seems that there is an issue with the spaces within the string... Any idea on how to fix this?字符串中的空格似乎有问题...知道如何解决这个问题吗?
Thanks a lot in advance,非常感谢,
Paul保罗
The issue is that you're probably (as you have posted no code) not passing the C:\Program Files\PTGui\ptgui.exe
as a string to the terminal but just a plain command问题是您可能(因为您没有发布任何代码)没有将C:\Program Files\PTGui\ptgui.exe
作为字符串传递给终端,而只是一个普通命令
Use利用
import os
os.system('\"C:\Program Files\PTGui\ptgui.exe\" -batch')
Like the os.system
documentation already tells you, a better solution altogether is to use subprocess
instead.就像os.system
文档已经告诉你的那样,一个更好的解决方案是使用subprocess
代替。
subprocess.run([
r"C:\Program Files\PTGui\ptgui.exe",
"-batch",
r"C:\Users\mw4168\OneDrive\Desktop\PTGui Tests\3 rows\panorama.pts"])
The string you created also lacked spaces between the indvidual arguments;您创建的字符串在 arguments 之间也缺少空格; but letting Python pass the arguments to the OS instead also gives you more control over quoting etc.但是让 Python 将 arguments 传递给操作系统也可以让您更好地控制引用等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.