How To Rename Files In Python

rename files in python image

Ever wondered if you could rename files in python programmatically without the hassles of picking them one by one?

These days web developers tends to make downloadable contents difficult to scrape or download in bulk automatically, which in return makes these downloadable contents titles look funny and somewhat long, with all the hashes and etc.

So I downloaded the full season 5 of vikings, one of my favorite series. I felt the need to shorten the titles so I can easily select and watch some episodes on my phone when am less busy.

I had to do it in a such a way that the episode numbers will not be altered as well. So my purpose was clear:

  • Reduce the length of the titles
  • Retain the file extension(.mp4)
  • Retain the season numbers, symbols and episode numbers

So I came up with a simple reusable function which I will use that time and subsequent times. So in this tutorial am going to share with you the codes and modules used.

ENOUGH STORIES! LET’S CODE:

We will begin by importing the necessary modules:

import os
from pprint import pprint

Yes that’s it! Just two modules is all we need, then we write our function. First let’s print out all the .mp4 files in our intended directory:

def rename_all():
    path = input('Please enter your folder path')
    files = os.listdir(path)
    for file in files:
        if file.endswith('.mp4'):
            print(file)

You will get an output like the following one:

Vikings - S05E01 HD (TvShows4Mobile.Com).mp4
Vikings - S05E03 HD (TvShows4Mobile.Com).mp4
Vikings - S05E04 HD (TvShows4Mobile.Com).mp4
Vikings - S05E09 HD (TvShows4Mobile.Com).mp4
Vikings - S05E18 HD (TvShows4Mobile.Com).mp4
Vikings - S05E19 HD (TvShows4Mobile.Com).mp4
Vikings - S05E20 HD (TvShows4Mobile.Com).mp4
Vikings - S05E17 HD (TvShows4Mobile.Com).mp4
Vikings - S05E16 HD (TvShows4Mobile.Com).mp4
Vikings - S05E15 HD (TvShows4Mobile.Com).mp4
Vikings - S05E13 HD (TvShows4Mobile.Com).mp4
Vikings - S05E14 HD (TvShows4Mobile.Com).mp4
Vikings - S05E12 HD (TvShows4Mobile.Com).mp4
Vikings - S05E11 HD (TvShows4Mobile.Com).mp4
Vikings - S05E10 HD (TvShows4Mobile.Com).mp4
Vikings - S05E08 HD (TvShows4Mobile.Com).mp4
Vikings - S05E07 HD (TvShows4Mobile.Com).mp4
Vikings - S05E06 HD (TvShows4Mobile.Com).mp4
Vikings - S05E05 HD (TvShows4Mobile.Com).mp4

According to our purpose for this function we need to do away with ‘Vikings –‘ and ‘(TvShows4Mobile.Com)‘ and leave only the rest alongside with the extension. Something like this: ‘S05E07 HD.mp4‘.

That’s where our programming sense comes in play:

Firstly we need to know how to rename in python, there are a couple of options for that but in this tutorial we will just use os.rename(). Then how do we make sure we do it in bulk without running into issues?

Since the os.rename() function takes in two parameters, source and destination, and they can all be strings. Then we will enter the files absolute path in source and slice the path to our intended result on the destination parameter.

# Rename multiple files in python
import os
from pprint import pprint

def rename_all():
    path = input('Please enter your folder path')
    files = os.listdir(path)
    for file in files:
        if file.endswith('.mp4'):
            os.rename(path+file, path+file[10:19]+'.mp4')
    print('Done renaming')
    pprint(os.listdir(path))

rename_all()

After running the function you will get an output like the following one:

Done renaming
['S05E03 HD.mp4',
 'S05E04 HD.mp4',
 'S05E09 HD.mp4',
 'S05E18 HD.mp4',
 'S05E19 HD.mp4',
 'S05E20 HD.mp4',
 'S05E17 HD.mp4',
 'S05E16 HD.mp4',
 'S05E15 HD.mp4',
 'S05E13 HD.mp4',
 'S05E14 HD.mp4',
 'S05E12 HD.mp4',
 'S05E11 HD.mp4',
 'S05E10 HD.mp4',
 'S05E08 HD.mp4',
 'S05E07 HD.mp4',
 'S05E06 HD.mp4',
 'S05E05 HD.mp4',
 'S05E01 HD.mp4']

So we sliced and added back our extension, and used the pprint to print our files in a nice order. Meanwhile there is no episode 2 because 1 is joined with 2.

Meanwhile, when entering your folder path, make sure you close it with a slash, forward or otherwise(according to your PC operating system), otherwise you will get an error.

Your slicing pattern or numbers might be different depending on the length of the episode title.

Now with this tutorial you have learnt one of the ways to rename files in python. You have also learnt about a few other stuffs like slicing, .endswith, os.rename and etc.

If you have questions , please don’t hesitate to ask me in the comment section, chat me up or write me directly. To also see more codes similar to how to rename files in python kindly check our tutorial post page.

🤞 Don’t miss the tips!

We don’t spam! Read more in our privacy policy

Geoff

Geoff is a python software engineer, web content specialist, tech private trainer and an IT virtual assistant.

Leave a Reply