delete a csv file in python

To delete a CSV file in Python, you can use the os library. Here's a code snippet that shows how to do it:

main.py
import os

file_path = "path/to/your/file.csv"

if os.path.exists(file_path):
    os.remove(file_path)
    print("File deleted successfully")
else:
    print("File not found")
176 chars
10 lines

In this code, we first import the os module, which provides a way to interact with the operating system.

Then we specify the path to the CSV file we want to delete. This can be an absolute or relative path.

We use the os.path.exists() function to check if the file exists. If it does, we use os.remove() to delete the file. We also print a message indicating whether the file was deleted successfully or not.

Note that once you delete a file, it cannot be recovered, so make sure you really want to delete it before running this code!

gistlibby LogSnag