Python- Rename files to snake_case.

Pip Bbaale
3 min readApr 3, 2021

--

Photo by Pip Bbaale

Being tasked to rename a number of files or directories can be quite a headache. In this case, I was tasked to rename a couple of images to snake case which can be quite frustrating as you have to add hyphens and use lower case as well.
Having watched a few youtube videos and read some articles, there was inefficient knowledge on the topic hence this article.

1. First things first.

What is Snake case?

Snake case refers to the style of writing in which each space is replaced by an underscore character, and the first letter of each word written in lowercase.

Having understood what snake case *i opened my favourite editor that is VS Code and made a clean outline of my idea in pseudocode.

function rename_images()
-get directory with files
-get and create new directory to store renamed files
- Loop through the files with a For Loop
- rename the files
-save renamed files to the new desired directory
call rename_images function()

2. Get Coding.

Firstly, we create our python file “rename_images.py” and import the modules we are going to use in our code.

import os
import shutil
import sys

  • os”- Provides functions for interacting with the operating system.
  • shutil”- Provides functions used to copy the content of source file to destination file or directory.
  • sys”- It lets us access system-specific parameters and functions.

We then move on to create our function to rename the files.

def rename_images(directory, new_directory):

Our function takes two parameters ie. the directory containing the files to be renamed and the new directory name where to save the renamed files.

Assigning our variables.

os.chdir(directory)
os.mkdir(new_directory)
save_path = new_directory

  • os.chdir”- Method used to set the current working directory.
  • os.mkdir”- Method used to create a directory named path with the specified numeric.
  • save_path”- Variable storing the directory path to save renamed files.

The Loop.

for f in os.listdir(directory):

A for loop to look through the each file in the directory and rename one by one.

File name separation.

f_name, f_ext = os.path.splitext(f)
f_title = f_name.replace(‘, ‘,’_’).replace(‘-’,’_’).replace(‘ ‘,’_’).lower()

  • First, we split the file name and extension and store them is different variables ie. f_name and f_ext.
  • We then go on to edit the f_name value and replace commas, hyphens and spaces with the underscore symbol.
  • We also include the .lower() method to make all letters lowercase.

File name transformation.

new_f_name = (‘{}{}’.format(f_title, f_ext.lower()))
shutil.copy(f, save_path+”/”+new_name)

  • We combine the new file name with its extension and also add .lower() method just in case.
  • we then use the shutil.copy() method to copy the renamed file and store in in the new directory.

Calling our function.

rename_images(sys.argv[1] , sys.argv[2])

We call the rename_image() function and pass in sys.argv methods from the sys module so as to accommodate comand line arguments.

3. Running our application.

In our terminal, we pass a command such as:

python rename_images.py “./images” “./img”

This command runs our python file “rename_images.py” in the [0] index and also takes in two values ie. “./images” in the [1] index and “./img” in the [2] index to be stored in the directory and new_directory variables respectively.

Code Snippet.

Code by Pip Bbaale

--

--

No responses yet