Published Apr 1, 2022Updated Apr 2, 2023
Contribute to Docs
.unlink() file method allows the user to delete a file path if it exists in the system or computer.
.remove() is the alternative to delete a specific file.
FileNotFoundError is raised if the file is not found or isn’t in the current directory.
This method is provided by the
os module which must be imported beforehand.
import os os.unlink("file_path")
Entire folders or directories can be removed using the
.unlink() to delete the my_file.txt file:
import os# Delete file pathos.unlink("path/to/my_file.txt")
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
Computer ScienceLooking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
Includes 6 Courses
With Professional Certification