This post will discuss how to rename a file in Python.

1. Using os.rename() function

A simple solution to rename a file in Python is using the os.rename() function.

Download Code

 
If the file is not present in the working directory, you need to specify its full path.

Download Code

 
If the source and destination directory are the same, i.e., you can do like:

Download Code

2. Using shutil.move() function

Alternatively, you can use the shutil module, which offers several high-level operations on files. You can use the shutil.move() function to rename or move a file.

Download Code

That’s all about renaming a file in Python.