Sometimes, we want to delete a file or folder in Python.
In this article, we’ll look at how to delete a file or folder in Python.
How to delete a file or folder in Python?
To delete a file or folder in Python, we can use the unlink
method.
For instance, we write
file_to_rem = pathlib.Path("/tmp/file_name.txt")
file_to_rem.unlink()
to create a path object with pathlib.Path
.
Then we call unlink
to remove the item at the given path.
Conclusion
To delete a file or folder in Python, we can use the unlink
method.