Currently I am working on an eCommerce website and I ran into a frequent problem, renaming files from vendors. Here is a quick and easy way to rename a large amount of files in Python!

A recent project required that I renamed a few thousand pdf’s to a standardized format. The vendor had supplied the instruction pdfs organized by item number as shown below however the filename contained additional information that makes it difficult to mass associate with products.

I simply need to take the sku (the number) remove everything else then add “-instructions” to the filename. With over 3000 of these files I would not want to do this by hand!

Python is a high level programming language whose interpreter can run on nearly any platform. The simplicity of the language has made it popular among a growing number of programmers in recent years. If you are not familiar with Python I suggest you checkout the PluralSight.com Path or read more at https://www.python.org

If you are new to Python you will need to download a installer for your OS. They have distributions of the interpreter for Mac, Windows and Linux. BTW an interpreter is the way Python converts human readable code into bytecode allowing the application to run quickly. Well.. that is kinda what it is, if you want to learn more I suggest Allison Kaptur’s article on AOSAbook.org.

Once you have Python installed you are ready to write and run code!

Below is some very very simple Python code which simply takes the file name and replaces anything after the hyphen (-) with “-instructions.pdf”

import os ##import the operating system object to use the rename method

import glob  ## import glob module to retrieve file lists

files = glob.glob('*.pdf') ##Find the files that match the pattern, in this case all pdf's in the directory

for file in files: 

    os.rename(file, '{}-instructions.pdf'.format(file.split('-')[0])) #loop through the files that match the pattern and split at each "-" then take the first part and delete the rest then add some -instruction.pdf to the first part## The rename function source filename and replaces it with the destination

First off we import a couple modules; os & glob. os gives us access to the operating system methods which has a rename() method and requires a source and destination.

Next we just get a list of the filenames in the current directory (you can also specify a path but we will run this from the same folder as our files) that match our simple expression. In this case I am looking for all pdf’s.

I can then loop through the list with a (for-in) statement to access the individual file names the use the split() method to explode the filename at the hyphen.

Finally I am passing the first part of the split filename (the part at index [0]) and appending “-instructions.pdf” to it and then the rename() method saves the modified filename.

To run the above script you just need to save the code to a file with a “.py” extension and save it in the same directory as the files you wish to modify. Make sure you have the Python terminal running and then just click to open the .py file as shown below and presto! Renamed files in a hurry!

With 2 mins worth of code I was able to save hours of tedious file renaming!

Here is another snippet I use often to get a list of filenames into a csv so i can then break it down however I want in GSheets.

import os
import csv

f = open("C:\Your\Path\To\document.csv",'r+')

w = csv.writer(f)

for path, dirs, files in os.walk("C:\Your\Path\To\Directory"):
    for filename in files:
        w.writerow([filename])

I love to use programming to make my everyday tasks quicker and easier. How do you tackle difficult or repetitive tasks at your job? Are you leveraging the power of modern computing?

I would love to hear about it so drop me a line or post a comment!