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.
os.remove(filePath)
This method accepts the following parameter:
filePath
: This is the path of the file.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)
os
module.filePath
that contains the file path.os.remove()
method to remove the file from the directory.