How to rename directory using Python?

PythonServer Side ProgrammingProgramming

You can rename a directory in Python by moving it using the shutil module. The shutil.move(src, dst) moves the directory from src to dst. If you just change the name of the directory without specifying the path, you'll basically be renaming it.

For example

>>> import shutil
>>> shutil.move('my_folder', 'new_name')

The above code renames my_folder to new_name. You could also use the os.rename(src, dst) for renaming directories.

For example

>>> import os
>>> os.rename('my_folder', 'new_name')
Updated on 13-Dec-2019 09:48:14