Use the os module’s rename() function to rename a file.

Example:

import os
source = 'a.txt'
destination = 'b.kml'
os.rename(source, destination)

This will rename the file a.txt to b.kml in the same directory.
If the file is in a specific path, provide the full path:

import os
source = '/path/to/a.txt'
destination = '/path/to/b.kml'
os.rename(source, destination)

Ensure the source file exists and that you have the necessary permissions to rename it.

Need Help With Python Development?

Work with our skilled Python developers to accelerate your project and boost its performance.

Hire Python Developers

Support On Demand!

Related Q&A