How to delete file with python | step-by-step guide in 2023

Python programming can be used for a variety of purposes. One such requirement is delete file with python. In this blog, we will learn how to interact with file systems using python. We will also learn how to delete/folders using various techniques. so let’s get started.

Delete file with python

Python provides various modules using which a user can delete a file or folder. The list of modules are:-

  • os module
  • Pathlib Module
  • shutil module

In the following session, we will understand how to use these modules to delete files/folders.

Python remove file

In this session, we will understand various ways using which a user can remove a file using python

Python remove file using the os module

I have the below files and folder present in my current directory

➜ ls -l
total 8
drwxr-xr-x  2 XXXXXX  staff  64 Dec 21 14:55 naive_skill
-rw-r--r--  1 XXXXXX  staff  12 Dec 21 14:56 test_python.txt

let’s try to remove the test_python.txt file.

import os
delete file with python

Python remove file using the pathlib module

Pathlib is another python library that can be used to delete a file. A user needs to create a path object first and then unlink it.

First, a user needs to import the pathlib module.

import pathlib
file = pathlib.Path("./test_python.txt")

Python remove a file if exists

Many times we need to verify if the file exists before performing the delete operation. We can use the OS module to verify the file’s existence before deletion.

import os
file= "./test_python.txt"
if os.path.exists(file):
  print("file is successfully removed")
  print("The file does not exist")

Since the file test_python.txt is present, so we get the output like the file is successfully removed

Python remove file if exists

Python remove all files

Let’s create 2 files under the current directory.

➜ ls -l  
total 8
drwxr-xr-x  2 XXXXXX  staff  64 Dec 21 14:55 naive_skill
-rw-r--r--  1 XXXXXX  staff  12 Dec 21 14:56 test_python1.txt
-rw-r--r--  1 XXXXXX  staff  12 Dec 21 14:56 test_python2.txt

Now to delete all the files we need a for loop to traverse through all files and delete them one by one. Below is the code for the same.

import os
dir = "."
for file in os.listdir(dir):
  os.remove(os.path.join(dir, file))
Python remove all files

Python delete directory

There are several ways to delete a directory in python. In this session, we will understand all of them.

Python delete directory using the os module

The os module only deletes the directory which is empty. It can not delete a non-empty directory. For this demo, I have created two directories. One of them is blank which other has a file present in it.

tree .                
├── naive_skill
│   └── hello.txt
└── test

Now let’s try to delete the empty directory

import os

Python delete directory

The test directory is successfully deleted. Now let’s try to delete a non-empty directory using the OS module.

we get the below error as the directory was non-empty.

Python delete directory

Python remove a non empty directory using the shutil module

Python provides another module called the shutil using which we can perform various file operations. The Shutil module offers high-level operation on a file.

Now let’s use the shutil module to delete the non-empty directory naive_skill

import shutil

shutil module will clean up the entire working tree irrespective of whether it is blank or not.

remove a non empty directory

Python delete directory if exists

Many times a user needs to verify the directory before deleting it. This can be achieved using both the os and shutil modules. Let’s try to delete a non empty directory naive_skill

Python delete directory if exists
import os
import shutil
dir = "./naive_skill"

if os.path.exists(dir):
    print("directory is successfully deleted")
	print("directory does not exists")

The above code returns “directory is successfully deleted” because the directory was present.

Python delete directory if exists


I hope you liked this detailed tutorial on delete file with python. Please do let me know if you need additional details.

More to Explore?

Python request post and get

Python substring

Python square root

Python NULL detailed tutorial

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top