Fix Python – How to rename a file using Python

Question

Asked By – zjm1126

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

Now we will see solution for issue: How to rename a file using Python


Answer

Use os.rename:

import os

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

Usage:

os.rename('from.extension.whatever','to.another.extension')

This question is answered By – YOU

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0