Renaming multiple video files using RE and OS

49 Views Asked by At

I recently downloaded a large number of mkv files with names formatted like "South Park S01E01 Cartman Gets an Anal Probe (640x480) [Phr0stY].mkv". I want to strip all of the text except for the actual episode names. Here is the code I have so far.

rootdir = '/Users/me/Documents/Test/'
for ii in os.listdir(rootdir):
    try:
        for kk in os.listdir(rootdir + ii):
            try:
                    new = re.sub('South\sPark\sS[0-9][0-9]E[0-9][0-9]\s', '', kk)
                    new1 = re.sub('\s\([0-9]+x[0-9]+\)\s\[Phr0stY\]', '', new)
                    os.rename(rootdir + ii + kk, rootdir + ii + new1)
            except:
                    pass
    except:
        pass

Everything works fine up until the os.rename line where it stops and causes the except clause to execute. When I print new1 the correct string is returned. Can anyone see why the rename isn't working?

1

There are 1 best solutions below

0
On BEST ANSWER

You should use os.path.join to combine the components of a file path, not +.