How to rename a file using Python How to rename a file using Python python python

How to rename a file using Python


Use os.rename:

import osos.rename('a.txt', 'b.kml')


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

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


import shutilshutil.move('a.txt', 'b.kml')

This will work to rename or move a file.