What is the os.remove() method in Python?
Overview
The os.remove() method in Python is used to remove a file at a specified path. If the specified path is of a directory, this method raises an OSError exception.
Syntax
os.remove(filePath)
Parameters
This method accepts the following parameter:
filePath: This is the path of the file.
Example
Let’s look at an example of the os.remove() method in the code snippet below:
# Import the OS moduleimport os# File pathfilePath = 'demo.txt'# Remove the fileos.remove(filePath)
Explanation
- Line 2: We import the
osmodule. - Line 5: We declare a variable
filePaththat contains the file path. - Line 8: We use the
os.remove()method to remove the file from the directory.