Automate The Boring Stuff: Renaming Files

April 17, 2018
python tutorial
Estimated Reading Time: 4 minute(s)


Prompt

In this blog post we’ll walk through how you can easily write a Python script to rename a series of files. This was written from inspiration from the book Automate The Boring Stuff, I have not read it but have heard very good things from it.

Imagine you went on vacation and took many photos. When you got back to finally plugging your camera into your computer, you realized every photo started with sony_, which you didn’t like. You could manually walk through and edit each file or we could write a simple Python script to remove this prefix for us.

Setup

We’ll be using the Terminal program which comes preinstalled on Mac and Linux machines. If you happen to be following this on a Windows machine, I suggest following a guide like this one to install Linux as a sub system, which will give you access to a Terminal.

  1. We’ll create a directory to perform this demo in. You can create this using your file explorer or finder, or can run the following command in Terminal:

    $ mkdir renaming_files

  2. Using our terminal, we’ll change our current directory to the one we just created:

    $ cd renaming_files

  3. We’ll create our fake files by running the following command in our terminal:

    $ touch sony_image_{0..1000}.png

    This will create one thousand files in our directory, starting with sony_image_0.png and ending with sony_image_1000.png

At this point our environment is setup.

Writing Our Script

Ensuring Python Is Installed

We’ll start by ensuring that Python is correctly installed, by running:

$ python --version

You should see some output that looks like

Python 3.6.4

or higher. If your version of Python is less than 3.6, trying running:

$ python3 --version

and if that works, simply use python3 for the rest of this guide.

If you get an error message like:

command not found: python

You’ll have to install Python before proceeding. There are many guides on how to do this and it will not be covered here.

Development

Using your favorite text editor, let’s start writing our script. We will save our script in the same directory as our files, for convenience. Our logic will be:

Note: The # represents a comment and is simply there to improve human readability. Our computer will ignore these.

# contains helpful premade actions we'll be using later.
# Anything that starts with os. came from this
import os

# gets a list of all the items in the current directory
file_names = os.listdir(".")  

# we'll loop over every file in the current directory
for file_name in file_names:
    # define the prefix we're looking to remove
    prefix = "sony_"

    # if this current file has the prefix we don't want
    if prefix in file_name:
        # create the new file name without the prefix
        new_file_name = file_name[len(prefix):]

        # print out for improved readability
        print(f"renaming {file_name} to {new__file_name}")

        # perform the actual renaming of the file
        os.rename(file_name, new_file_name)

We’ll save this file as automating_the_boring_stuff_renaming_files.py and now we’re ready to run our code.

Execution

To execute our code, in terminal write: $ python automating_the_boring_stuff_renaming_files.py. We should see a line printout for each file that we renamed, something like:

renaming sony_image_001.png to image_001.png

Once the script finishes, we can double check that it worked as expected by running the following in terminal:

ls

This should list all the files in the directory, which now should not have the sony_ prefix in the file name.

Conclusion

This is a super simple script coming in at under twenty five lines, including comments, but automates what would have taken a long time and been incredibly boring. I hope this motivates at least one person to give programming a try, as it truly is a powerful tool.

Python Dependency Packaging

September 30, 2019
python distribution

Python, Pandas and SQL (Oh my!)

July 11, 2019
pandas python sql databases

Visualizing Geospatial Data

April 22, 2018
python tutorial visualization
comments powered by Disqus