Renaming directories in Windows can be a tedious and time-consuming task, especially when dealing with multiple sub-directories. Automation for such cases is not just a convenience, it’s a necessity. Python’s Standard Library offers a powerful yet straightforward solution with the os
module. This blog post is aimed at Python enthusiasts who are interested in learning how to rename sub-directories on a Windows system using Python.
Prerequisites
Before we begin, make sure you have Python installed on your Windows machine. You can download and install Python from the official website or refer to my blog post . Once you have installed Python, you can test the installation by opening the command prompt and typing:
python --version
Understanding the Code
The following code demonstrates how to rename Windows sub-directories using the Python Standard Library os
:
import os
f = 'D:\MyFolder\My Work'
old = '----'
new = ''
for path, subdirs, files in os.walk(f, topdown=False):
for name in subdirs:
if old in name:
file_path = os.path.join(path, name)
new_name = os.path.join(path, name.replace(old, new))
os.rename(file_path, new_name)
Let’s break down the code and understand each line.
Import the os
module
import os
The first step is to import the os
module, which is included in Python’s Standard Library and provides functions for interacting with the operating system.
Define the root directory
f = 'D:\MyFolder\My Work'
Replace D:\MyFolder\My Work
with the path of the root directory where you want to start the search for sub-directories.
Define the old and new names
old = '----'
new = ''
Next, specify the placeholder text you’d like to change in the names of the sub-directories. Old contains the text to be replaced, and new contains the new text that will take its place.
Use os.walk()
to traverse the directory tree
for path, subdirs, files in os.walk(f, topdown=False):
os.walk()
generates the file names in a directory tree by walking the tree either top-down or bottom-up. It outputs a list of three tuples containing a directory path, a list of subdirectories, and a list of files.
In this case, we are using topdown=False
to traverse the directory tree in a bottom-up manner. This is important when renaming directories, as the references to sub-directory paths will change as we rename.
Iterate through the sub-directories
for name in subdirs:
This loop iterates through all the sub-directories in the current directory.
Check if the old name exists in the sub-directory name
if old in name:
This line checks if the old name exists in the current sub-directory name.
Construct the file path and new name
file_path = os.path.join(path, name)
new_name = os.path.join(path, name.replace(old, new))
os.path.join()
is used to construct the file path and new name by replacing the old name with the new name.
Rename the sub-directory
os.rename(file_path, new_name)
This line renames the sub-directory from the old name to the new name.
You now have a convenient script to automate the renaming of multiple sub-directories in Windows using Python’s os module. This capability is particularly useful for content creators who manage a large number of files and directories. With a bit of customization, this script can be adapted to handle a variety of file management tasks.
Remember that file system operations are significant and can have irreversible effects. Always ensure you have backups before running such scripts on important data.
I hope this blog post has been helpful in showing you how to rename Windows sub-directories using the Python Standard Library os. If you have any questions or comments, please leave them below.
To learn more about these tips and articles, please follow me on Linkedin or Twitter.