Remove A File

To remove a file in Python, we can use os module and use its os.remove() function to do that.

Example

import os
os.remove("filename.txt")

Note: Error(s) Ahead! If going with this approach, so better is to test first if the file exists.

import os
if os.path.exists("filename.txt"):
    os.remove("filename.txt")
    print("filename.txt deleted successfully.")
else:
    print("filename.txt not found.")

Source: W3Schools

Last updated