Search code examples
pythonsubprocesspopen

subprocess.Popen(["open", "target.mkv"],shell=True fail to open a specified file


I am learning subprocess to open multimedia ".mkv" files,

Firstly, I test:

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]

All that happened was the prompt usage message.

Then, I tried:

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

It worked and opened the target file immediately,

I noticed that the first option of [] is preferable than the latter "string" option.

What's the problem with the [] option failing to open a file?


Solution

  • From the subprocess.Popen Constructor (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.