I Used This Python Script to Rename All Files in Subfolders (Recursive)

5/5 - (1 vote)

I’m currently working on a website project for the Finxter ecosystem, where I’m given a folder with subfolders (and subfolders within) that contain image files.

Example folder structure:

-- subfolder1
---- image1.jpg
---- image2.jpg
---- image3.jpg
-- subfolder2

Across the subfolders, the files may have the same name, which causes an issue when uploading all files to my webserver. To fix all issues, I want to rename all files, so they have unique names.

πŸ’‘ Project Goal: My simple idea was to add a prefix with the folder structure in front of each file name. For example, a file named 'myfile1.jpg' in folder 'myfolder1' will be renamed to the new file 'myfolder1_myfile1.jpg'. All the files with new names should be copied into a subfolder as specified in a destination variable.

Desired output folder structure:

---- subfolder1_image1.jpg
---- subfolder1_image2.jpg
---- subfolder1_image3.jpg
---- subfolder2_image1.jpg
---- subfolder2_image2.jpg

Here’s the code to accomplish this — I’ll explain it in a minute! πŸ‘‡

# import os and sys modules
import os, sys
import shutil

# get the root directory from command line argument
root_dir = '.'
destination = './my_destination'

# walk through the directory tree
for dirname, subdirs, files in os.walk(root_dir):
    # for each directory, print the directory's path
    # for each file in the directory, copy and rename
    for file in files:
        new_name = dirname[2:].replace('\\', '_') + "_" + file
        shutil.copy(os.path.join(dirname, file),
                    os.path.join(destination, new_name))

You need to give absolute or relative paths from the perspective of this Python file. It’ll already run fine if the Python file is stored in the same folder as the subfolders. But you need to have a subfolder called my_destination.

How does this code work?

This code recursively copies all files in a directory tree to a specified destination folder.

You take the root and destination directories and iterate through each directory and subdirectory within the root directory. For each directory, you print out the directory’s path.

For each file in the directory, you copy it to the specified destination folder, renaming it to include the path of the original directory in the name.

I used string slicing and the string.replace() method to modify the destination filenames according to my goals, see the following line:

new_name = dirname[2:].replace('\\', '_') + "_" + file

Note that the double backslash \\ is actually an (escaped) single backslash character that you usually don’t want in your new filename. We replace all of this with single underscores such that a file myDir\myFile becomes myDir_myFile.