Ducktor
Ducktor

Reputation: 335

Changing name of multiple files with conditions

I got some episodes of a show downloaded onto my PC, but the titles are mixed up completely so it's hard to find the episode I want and I don't want to refer to the wiki to find out which episode is which

Since there's a too many episodes to individually rename them, I've decided to use python to rename all at once, using a json table to store the correct episodes name to compare and replace them (the episodes all have their season number and episode number so I can use that for the comparison)

Currently, I've got this:

import os
import sys
from random import randint as mathRandom
nameDict={}

#nameDict["Ed.Edd.n.Eddy.S01E01"]={"Name":"Ed Touchables / Nagged to Ed"}
nameDict["Ed.Edd.n.Eddy.S01E02"]={"Name":"Pop Goes the Ed / Over Your Ed"}
nameDict["Ed.Edd.n.Eddy.S01E03"]={"Name":"Sir Ed-a-Lot / A Pinch to Grow an Ed"}
nameDict["Ed.Edd.n.Eddy.S01E04"]={"Name":"Dawn of the Eds / Virt-Ed-Go"}
nameDict["Ed.Edd.n.Eddy.S01E05"]={"Name":"Read All About Ed / Quick Shot Ed"}
nameDict["Ed.Edd.n.Eddy.S01E06"]={"Name":"An Ed Too Many / Ed-n-Seek"}
nameDict["Ed.Edd.n.Eddy.S01E07"]={"Name":"Look into My Eds / Tag Yer Ed"}
nameDict["Ed.Edd.n.Eddy.S01E08"]={"Name":"Fool on the Ed / A Boy and His Ed"}
nameDict["Ed.Edd.n.Eddy.S01E09"]={"Name":"It's Way Ed / Laugh Ed Laugh"}
nameDict["Ed.Edd.n.Eddy.S01E10"]={"Name":"A Glass of Warm Ed / Flea-Bitten Ed"}
nameDict["Ed.Edd.n.Eddy.S01E11"]={"Name":"Who, What, Where, Ed! / Keeping Up with the Eds"}
nameDict["Ed.Edd.n.Eddy.S01E12"]={"Name":"Eds-Aggerate / Oath to an Ed"}
nameDict["Ed.Edd.n.Eddy.S01E13"]={"Name":"Button Yer Ed / Avast Ye Eds"}

path = 'C:/Users/badfitz66/Desktop/EdEddnEddy/Episodes'

os.chdir(path)

for filename in os.listdir(path):
    filename_splitext = os.path.splitext(filename)

    newSettings = nameDict[filename_splitext[0]].get("Name")
    if  newSettings is not None :
        if nameDict.get(filename_splitext[0]):
            os.rename(filename, filename + str(newSettings)+'.mkv')

I'm trying to change the episodes names that are the first values (eg: Ed.Edd.n.Eddy.S01E04) to the episode number and season + the name of each part (eg: Read All About Ed / Quick Shot Ed)

But when I run, I get this error:

Traceback (most recent call last):
  File "C:/Users/badfitz66/Desktop/ah.py", line 33, in <module>
    os.rename(filename, filename + str(newSettings)+'.mkv')
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'Ed.Edd.n.Eddy.S01E02.mkv' -> 'Ed.Edd.n.Eddy.S01E02.mkvPop Goes the Ed / Over Your Ed.mkv'

What am I doing wrong?

Upvotes: 2

Views: 102

Answers (2)

Priyesh Kumar
Priyesh Kumar

Reputation: 2857

In your for loop, episode is a string, not a dict. So you cannot access its elements using string index. Just replace episode['Name'] with episode, and it renamed all my files.

You can use only 1 loop to get things done

import os
import sys

nameDict={}

nameDict["test.1"]={"Name":"LOL1.mkv"}
nameDict["test.2"]={"Name":"LOL2.vcl"}
nameDict["test.3"]={"Name":"LOL3.txt"}
nameDict["test.4"]={"Name":"LOL4.py"}
nameDict["test.5"]={"Name":"LOL5.ss"}
nameDict["test.6"]={"Name":"LOL6.lol"}
nameDict["test.7"]={"Name":"LOL7.po"}

path = 'test'

# Loop over all files and directories
for filename in os.listdir(path):
    # Get the name
    filename_splitext = os.path.splitext(filename)
    # print filename_splitext

    # Check if we have new file name in nameDict
    newSettings = nameDict.get( str(filename_splitext[0]), None) 
    if  newSettings is not None :
        # print newSettings
        # Rename with new filename
        os.rename(os.path.join(path, filename),os.path.join(path, newSettings['Name']))

Upvotes: 1

justderb
justderb

Reputation: 2854

Shot in the dark:

Try episode.Name instead of episode["Name"]

Upvotes: 0

Related Questions