Renaming and Deleting Files in Python
Renaming and Deleting Files in Python
Python has a large set of the module which provides various methods to fulfill the operations like renaming and deleting the file in the machine. In order to use that method, we need to import the module first and after then we can use those methods.
The remove() method in python:
You can easily delete the files using the remove() method as putting the file name in the arguments.
Example:
# Delete the file top.txt
or.remove("top.txt")
The rename() method in python:
This method basically takes 2 arguments, current file name, and new filename respectively. By providing both the file name we can easily rename the file easily.
Example:
import os
# Rename a file from top.txt to pot.txt
os.rename( "top.txt", "pot.txt" )
