Renaming a file to match parent folder name in Python 2.x.x

I keep all of my movies on a home NAS and don’t even remember the last time I used an actual disc to watch a movie.  I currently use MakeMKV (runs in Linux beautifully) to dump the films to my organized folder structure.  However, the file names it produces are usually not what I’m looking for – things like title00.mkv.  However, if there are multiple files in a folder, I generally want these to be left alone as they are usually extras (behind the scenes, interviews, that sort of thing).  So, I wrote a quick little app in Python to walk my directory structure, and if it finds a single file within a folder, it renames the file to match the parent folder name.  Now “title00.mkv” becomes “Blade Runner.mkv”.


This code runs in Python 2.x.x, hope someone finds this useful.  It will accommodate spaces in your paths in all OSes.  Simple to use, just provide the absolute path (from the root) via the ROOT_DIR variable.  It will begin in this folder and recursively walk/rename.  In Windows this path might look like:

ROOT_DIR = r’c:\users\yourusername\Desktop\media_folder’



Leave a Reply

Your email address will not be published. Required fields are marked *