How to delete file in Python(5 ways)

In this post, we are going to learn How to delete file in Python that can be single files or files from a directory. os.rmdir() method deletes the file from the directory or folder, sometimes we have to delete all files and folders in a directory.

1. How to Delete file if exist in Python


When we think of deleting a file, questions comes to our mind , which function is used to delete a file in Python. The Python os module remove() method is used to remove a file from a given file path. The os.remove() method takes an argument the path of the file we want to delete. To use this method first we have to import the OS module.

We are checking if file exist using exists() method and deleting it using os.remove(),It raise FileNotFoundError if file not exist.

Python Program: How do you delete a file in Python path.

import os
if os.path.exists("devenum.txt"):
  os.remove("deveum.txt")
  print('File deleted sucessfully')
else:
  print("file does not exist")

Output

File deleted sucessfully

2.How to delete empty directory using OS.rmdir()


os.rmdir() method delete an empty folder or directory.The OS.rmdir() takes an argument of the path of a directory that we want to delete. The directory should be an empty one otherwise it raises OSError.It raises Permission Error does not have permission to access the file.

Python Program to delete empty directory using OS.rmdir()

import os

path = os.path.join(os.getcwd(),'pyth')

try:
 if os.path.exists(path):
   os.rmdir(path)
   print('folder deleted')
  
 else:
   print("folder does not exist")
except PermissionError as error:
    print('Do not permission to delete folder')
except OSError as error:
    print('Error while deleting folder')

Output

Do not permission to delete folder

3. Python shutile module to delete files in directory


The shuttle module rmtree() method is used to delete a directory and its contents(files and folders). It takes an argument path of a directory and deletes entirely directory tree recursively.

Python Program to delete files in direcory

import os
import shutil

path = os.path.join(os.getcwd(),'data')

try:
 if os.path.exists(path):
   shutil.rmtree(path)
   print('file and folders are deleted')
  
 else:
   print("folder does not exist")
except PermissionError as error:
    print('Do not permission to delete folder')
except OSError as error:
    print('Error while deleting folder')

Output

file and folders are deleted

4. How to delete file Pathlib


The pathlib module unlink() method is used to delete a file. It is added to python version 3.4+. Let us understand with the below example.

Python Program Pathlib to delete file

import pathlib

file = pathlib.Path("studata.txt")

file.unlink()

The pathlib module rmdir() method is used to delete an empty folder. Let us understand with the below example

Python Program Pathlib to delete file

import pathlib
path = pathlib.Path("pyth")
path.rmdir()

5. os.remove() delete Mutiple files based on extension


To delete files based on a specific extension we will use the glob module glob() method. First, we will get the path of all files as a list that will match the given pattern. We will iterate over each file path and delete each file using os.remove() method or we can use pathlib module file.unlike() method

Python Program

import glob, os

files_path = glob.glob('*.txt')
print(files)

for file in files_path:
    os.remove(file)

6. Delete Mutiple files in folder based on Extension


To delete files based on a specific extension we will use the glob module glob() method in a folder path. We are using the current directory(path = os.getcwd()) where we want to delete all files with the given extension by using

First, we will get all the path lists of all To delete files based on a specific extension we will use the glob module glob() method. First, we will get the path of all files as a list that will match the given pattern. We will iterate over each file path and delete each file using the os.remove() method or we can use pathlib module file.unlike() method.

Python Program

import glob, os

path = os.getcwd()

files_path = glob.glob('*.txt')


for file in files_path:
    os.remove(path+file)


Summary

In this post, we have learned that How to delete file in Python by using the os.pathlib, os.shuitle module that can be single or multiple files.