Skip to main content
 首页 » 编程设计

python之subprocess.Popen( ["open", "target.mkv"],shell=True 打开指定文件失败

2024年12月31日7unruledboy

我正在学习 subprocess 打开多媒体“.mkv”文件,

首先,我测试:

In [25]: subprocess.Popen( 
    ...:     ["open", "/Volumes/Transcend/Downloads/The.Adventure.of.English.Ep4.mkv"], shell=True) 
    ...:      
Out[25]: <subprocess.Popen at 0x10b063908> 
In [26]: Usage: open [-e] [-t] [-f] [-W] [-R] [-n] [-g] [-h] [-s <partial SDK name>][-b <bundle identifier>] [-a <application>] [filenames] [--args arguments] 

所发生的只是提示使用信息。

然后,我尝试了:

In [27]: subprocess.Popen('open "/Volumes/Transcend/Downloads/The.Adventure.of.English.Ep4.mkv"', shell=True) 
Out[27]: <subprocess.Popen at 0x10b114208> 

它成功了,并立即打开了目标文件,

我注意到 [] 的第一个选项比后面的“string”选项更可取。

[] 选项无法打开文件有什么问题?

请您参考如下方法:

来自 subprocess.Popen 构造函数 ( Docs ):

The shell argument (which defaults to False) specifies whether to use the shell as the program to execute. If shell is True, it is recommended to pass args as a string rather than as a sequence.