Reputation: 61
I'm attempting to build a script to batch process and rename videos according to their content, and am attempting to call ffprobe from python in order to do so.
For filenames that have spaces, ffprobe is OK with either giving them a '' or putting the whole string in ' ' .
At the terminal command line both of the following work:
ffprobe 'deleteMe 2.mp4'
ffprobe deleteMe\ 2.mp4
...and ffprobe outputs the expected result.
But of the following code, only the first example, where the filename does not have spaces, actually works:
fileName = 'deleteMe.mp4'
fileString = fileName.replace(' ','\ ')
result1 = subprocess.run(['ffprobe', fileString], capture_output=True, encoding='UTF-8')
print(result1.stderr)
fileName = 'deleteMe 2.mp4'
fileString = fileName.replace(' ','\ ')
result2 = subprocess.run(['ffprobe', fileString], capture_output=True, encoding='UTF-8')
print(result2.stderr)
fileString = '\'' + fileName + '\''
result3 = subprocess.run(['ffprobe', fileString], capture_output=True, encoding='UTF-8')
print(result3.stderr)
The other two return a 'No such file or directory' error in the stderr.
Upvotes: 0
Views: 37
Reputation: 141
Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.
Therefore, you don't need to escape the space, or you can escape it and set shell argument as true.
Reference: https://docs.python.org/3/library/subprocess.html#frequently-used-arguments
Upvotes: 1