How to rename a file using Python

I want to change a.txt to b.kml.


Use os.rename:

import os

os.rename('a.txt', 'b.kml')

File may be inside a directory, in that case specify the path:

import os
old_file = os.path.join("directory", "a.txt")
new_file = os.path.join("directory", "b.kml")
os.rename(old_file, new_file)