What does the os.remove() function do in Python?
a) Deletes a file from the filesystem
b) Deletes a directory from the filesystem
c) Renames a file or directory
d) Copies a file to a new location
Answer:
a) Deletes a file from the filesystem
Explanation:
The os.remove()
function in Python is used to delete a file from the filesystem. This function is part of the os
module and requires the file path as an argument. If the file does not exist, a FileNotFoundError
is raised.
import os
# Example of deleting a file
os.remove("example.txt")
In this example, the file example.txt
is deleted from the filesystem using the os.remove()
function.
Deleting files programmatically is useful for cleaning up temporary files, removing outdated data, or managing file storage in applications.