Full Stack LAMP - MEAN Developer, Python developer. Certified Azure Developer. Freelance programmer/consultant/trainer.

How to Delete a File in Python

In this tutorial, you will learn how to delete a file using the Python programing language. Deleting files using Python is very simple. You can delete files using the os.remove() method. You can delete a file using both relative path and absolute path. Below is the example code.

Remove file with relative path

import os

# removing a file with relative path
os.remove("file_1.txt")

Remove file with absolute path

import os

# remove file with absolute path
os.remove(r"c:\test\file_2.txt")