Categories
Python Answers

How to delete a file or folder in Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *