Python os module provides methods that help you perform file-processing operations, such as renaming and deleting files.
To use this module you need to import it first and then you can call any related functions.
The rename() method takes two arguments, the current filename and the new filename.
Following is the example to rename an existing file test1.txt −
#!/usr/bin/python import os # Rename a file from test1.txt to test2.txt os.rename( "test1.txt", "test2.txt" )
You can use the remove() method to delete files by supplying the name of the file to be deleted as the argument.
Following is the example to delete an existing file test2.txt −
#!/usr/bin/python import os # Delete file test2.txt os.remove("text2.txt")