[Tutor] Using subprocess on a series of files with spaces

Emile van Sebille emile at fenx.com
Thu Jul 31 22:04:57 CEST 2014


On 7/31/2014 12:53 PM, C Smith wrote:
> I am on OSX, which needs to escape spaces in filenames with a backslash.
> There are multiple files within one directory that all have the same
> structure, one or more characters with zero or more spaces in the
> filename, like this:
> 3 Song Title XYZ.flac.
> I want to use Python to call ffmpeg to convert each file to an .mp3.
> So far this is what I was trying to use:
> import os, subprocess
> track = 1
> for filename in os.listdir('myDir'):
>      subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
>      track += 1

You might try using '"%s"' % filename so that the name is within quotes 
for the shell environment.

Emile




More information about the Tutor mailing list