如何从 Python 执行程序? os.system 由于路径中存在空格而失败
我有一个Python脚本需要执行外部程序,但由于某种原因失败了。
如果我有以下脚本:
import os;
os.system("C:\\Temp\\a b c\\Notepad.exe");
raw_input();
那么它会失败并出现以下错误:
“C:\Temp\a”未被识别为内部或外部命令、可操作程序或批处理文件。
如果我用引号转义程序:
import os;
os.system('"C:\\Temp\\a b c\\Notepad.exe"');
raw_input();
那么它就可以工作。 但是,如果我添加一个参数,它会再次停止工作:
import os;
os.system('"C:\\Temp\\a b c\\Notepad.exe" "C:\\test.txt"');
raw_input();
执行程序并等待其完成的正确方法是什么? 我不需要读取它的输出,因为它是一个可视化程序,完成工作然后退出,但我需要等待它完成。
另请注意,将程序移动到非间隔路径也不是一个选项。
这也不起作用:
import os;
os.system("'C:\\Temp\\a b c\\Notepad.exe'");
raw_input();
注意交换的单/双引号。
无论这里有或没有记事本参数,它都会失败并显示错误消息
文件名、目录名或卷标语法不正确。
I have a Python script that needs to execute an external program, but for some reason fails.
If I have the following script:
import os;
os.system("C:\\Temp\\a b c\\Notepad.exe");
raw_input();
Then it fails with the following error:
'C:\Temp\a' is not recognized as an internal or external command, operable program or batch file.
If I escape the program with quotes:
import os;
os.system('"C:\\Temp\\a b c\\Notepad.exe"');
raw_input();
Then it works. However, if I add a parameter, it stops working again:
import os;
os.system('"C:\\Temp\\a b c\\Notepad.exe" "C:\\test.txt"');
raw_input();
What is the right way to execute a program and wait for it to complete? I do not need to read output from it, as it is a visual program that does a job and then just exits, but I need to wait for it to complete.
Also note, moving the program to a non-spaced path is not an option either.
This does not work either:
import os;
os.system("'C:\\Temp\\a b c\\Notepad.exe'");
raw_input();
Note the swapped single/double quotes.
With or without a parameter to Notepad here, it fails with the error message
The filename, directory name, or volume label syntax is incorrect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
subprocess.call
将避免必须处理各种 shell 的引用约定的问题。 它接受一个列表,而不是一个字符串,因此参数更容易分隔。 IEsubprocess.call
will avoid problems with having to deal with quoting conventions of various shells. It accepts a list, rather than a string, so arguments are more easily delimited. i.e.这是一种不同的方法。
如果您使用的是 Windows,则以下操作类似于在资源管理器中双击文件,或将文件名作为 DOS“启动”命令的参数:使用与其扩展名关联的任何应用程序(如果有)打开该文件。
示例:
如果记事本与 .txt 文件关联,这将使用记事本打开 textfile.txt。
Here's a different way of doing it.
If you're using Windows the following acts like double-clicking the file in Explorer, or giving the file name as an argument to the DOS "start" command: the file is opened with whatever application (if any) its extension is associated with.
Example:
This will open textfile.txt with Notepad if Notepad is associated with .txt files.
最外面的引号由 Python 本身使用,Windows shell 看不到它。 如上所述,Windows 只理解双引号。
Python 将在 Windows 上将正斜杠转换为反斜杠,因此您可以使用
' 由 Python 使用,然后传递“C://Temp/abc/Notepad.exe”(作为 Windows 路径,不需要双反斜杠)到CMD.EXE
The outermost quotes are consumed by Python itself, and the Windows shell doesn't see it. As mentioned above, Windows only understands double-quotes.
Python will convert forward-slashed to backslashes on Windows, so you can use
The ' is consumed by Python, which then passes "C://Temp/a b c/Notepad.exe" (as a Windows path, no double-backslashes needed) to CMD.EXE
至少在 Windows 7 和 Python 3.1 中,如果命令路径中有空格,Windows 中的 os.system 需要命令行用双引号引起来。 例如:
一个令我困惑的现实例子是在 VirtualBox 中克隆驱动器。 由于某些访问权限问题,上面的 subprocess.call 解决方案不起作用,但是当我双引号该命令时,os.system 变得很高兴:
At least in Windows 7 and Python 3.1,
os.system
in Windows wants the command line double-quoted if there are spaces in path to the command. For example:A real-world example that was stumping me was cloning a drive in VirtualBox. The
subprocess.call
solution above didn't work because of some access rights issue, but when I double-quoted the command,os.system
became happy:对于 python >= 3.5 应使用
subprocess.run
代替subprocess.call
https://docs.python.org/3/library/subprocess.html#older-high-level-api
For python >= 3.5
subprocess.run
should be used in place ofsubprocess.call
https://docs.python.org/3/library/subprocess.html#older-high-level-api
我怀疑这与您在 Windows 中使用快捷方式时遇到的问题相同...尝试以下操作:
I suspect it's the same problem as when you use shortcuts in Windows... Try this:
对于 Python 3.7,请使用 subprocess.call 。 使用原始字符串来简化 Windows 路径:
For Python 3.7, use subprocess.call. Use raw string to simplify the Windows paths:
假设我们要运行 Django Web 服务器(在 Linux 中),路径之间有空格 (path=
'/home//;'
),因此请执行以下操作:[注意]:
chmod 755 -R <'您的路径'>
chmod +x manage.py
Suppose we want to run your Django web server (in Linux) that there is space between your path (path=
'/home/<you>/<first-path-section> <second-path-section>'
), so do the following:[Note]:
chmod 755 -R <'yor path'>
manage.py
is exceutable:chmod +x manage.py
不需要子进程,可以简单地实现
No need for sub-process, It can be simply achieved by