rename a directory in python

To rename a directory in Python, we can use the os module's rename() function or the shutil module's move() function with the os.rename() method.

Here's an example using os.rename():

main.py
import os

old_dir_name = "old_directory_name"
new_dir_name = "new_directory_name"

os.rename(old_dir_name, new_dir_name)
122 chars
7 lines

Alternatively, we can use the shutil.move() function like this:

main.py
import shutil

old_dir_name = "old_directory_name"
new_dir_name = "new_directory_name"

shutil.move(old_dir_name, new_dir_name)
128 chars
7 lines

Both methods will rename the directory named "old_directory_name" to "new_directory_name". Make sure to replace the values of old_dir_name and new_dir_name with the actual names of your directories.

gistlibby LogSnag