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

C Smith illusiontechniques at gmail.com
Thu Jul 31 21:53:48 CEST 2014


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


I am about to use re.sub to just replace all the '/s' with '\\/s', but
is there a simpler/more pythonic way to do this?


More information about the Tutor mailing list