Explain how to delete a file in Python?

Last updated 3 weeks, 6 days ago | 18 views 75     5

Tags:- Python

Python | Delete a file

Using command  os.remove(file_name)

 

Use the os module to delete a file, we need to first import it, and then use the remove() function provided by the module to delete the file. It takes the file path as a parameter.

import os
file_path = <file_path>
if os.path.isfile(file_path):
    os.remove(file_path)