Renaming and deleting files in Python is even easier than reading and writing to a file. The first thing you need to do is to import the
os module. The
rename() functions are in this module.
Let’s take a look at the example:
import os os.rename('example.txt','renamed.txt')
First we import the
os module then we tell the function
rename() within the newly imported
os module to rename the file
That’s all it is. Your file should be renamed now.
It is the same thing when you want to delete a file.
import os os.remove('renamed.txt')
You can easily remove a file using the command