remove an existing file in python

To remove an existing file in Python, we can use the os module which provides a function called remove(). The syntax for using this function is as follows:

main.py
import os

# specify the file path
file_path = "/path/to/file.txt"

# remove file
os.remove(file_path)
103 chars
8 lines

Here, os.remove() takes a file path as an argument and deletes the file at that path.

It's important to note that this operation is irreversible and will permanently delete the file, so use it with caution.

gistlibby LogSnag