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')
Published on 26-Dec-2017 10:30:02