Delete (Remove) Files and Directories in Python (2024)

In this tutorial, you’ll learn how to delete files or directories in Python.

After reading this tutorial, you’ll learn: –

  • Deleting file using the os module and pathlib module
  • Deleting files from a directory
  • Remove files that match a pattern (wildcard)
  • Delete empty directory
  • Delete content of a directory (all files and sub directories)

Sometimes we need to delete files from a directory that is no longer needed. For example, you are storing monthly inventory data in a file. You may want to delete any existing data files before creating a new data file each month.

Also, after some time, the application needs to delete its old log files.

In this tutorial, we will use the following Python functions to delete files and folders.

FunctionDescription
os.remove('file_path')Removes the specified file.
os.unlink('file_path')Removes the specified file. Useful in UNIX environment.
pathlib.Path("file_path").unlink()Delete the file or symbolic link in the mentioned path
os.rmdir('empty_dir_path')Removes the empty folder.
pathlib.Path(empty_dir_path).rmdir()Unlink and delete the empty folder.
shutil.rmtree('dir_path')Delete a directory and the files contained in it.

Note:

  • All above functions delete files and folders permanently.
  • The pathlib module was added in Python 3.4. It is appropriate when your application runs on a different operating systems.

Table of contents

  • How to Delete a File in Python
    • Example: Remove File in Python
    • Understand the os.remove() method
    • Check if File Exist Before Deleting It
  • Remove File Using os.unlink() method
  • Pathlib Module to Remove File
  • Delete all Files from a Directory
    • Delete an Empty Directory (Folder) using rmdir()
    • Delete a Non-Empty Directory using shutil
  • Deleting Files Matching a Pattern
    • Example: Deleting Files with Specific Extension
    • Delete file whose name starts with specific string
    • Delete file whose name contains a specific letters
    • Deleting Files Matching a Pattern from all Subfolders
  • Conclusion

How to Delete a File in Python

Python provides strong support for file handling. We can delete files using different methods and the most commonly used one is the os.remove() method. Below are the steps to delete a file.

  1. Find the path of a file

    We can delete a file using both relative path and absolute path. The path is the location of the file on the disk.
    An absolute path contains the complete directory list required to locate the file. And A relative path includes the current directory and then the file name.
    For example,/home/Pynative/reports/samples.txtis an absolute path to discover the samples.txt.

  2. Use os.remove() function to delete File

    The OS module in Python provides methods to interact with the Operating System in Python. The remove() method in this module is used to remove/delete a file path.
    First, import the os module and Pass a file path to the os.remove('file_path') function to delete a file from a disk

  3. Use the rmtree() function of shutil module to delete a directory

    Import the shutil module and pass the directory path to shutil.rmtree('path') function to delete a directory and all files contained in it.

Example: Remove File in Python

The following code explains how to delete a file named “sales_1.txt”.

Let’s assume we want to delete the sales_1.txt file from the E:\demos\files\ directory. Right now, this directory contains the following files:

  1. sales_1.txt
  2. sales_2.csv
  3. profits.txt
  4. revenue.txt

Remove file with relative path

import os# removing a file with relative pathos.remove("sales_1.txt")Code language: Python (python)

Remove file with absolute path

import os# remove file with absolute pathos.remove(r"E:\demos\files\sales_2.txt")Code language: Python (python)

Our code deleted two files. Here is a list of the remaining files in our directory:

  • profits.txt
  • revenue.txt

Understand the os.remove() method

Syntax:

os.remove(path, *, dir_fd = None)Code language: Python (python)

Pass file path to the os.remove('file_path') function to delete a file from a disk

The following are the parameters that we need to pass.

  • path – A relative or absolute path for the file object generally in string format.
  • dir_fd – A directory representing the location of the file. The default value is none and this value is ignored in the case of an absolute path.

If the passed file path is a directory, an OSError will be raised

Check if File Exist Before Deleting It

A FileNotFoundError will be raised if the file is not found in the path so it is advisable to check if the file exists before deleting it.

This can be achieved in two ways:

  • os.path.exists("file path") function to check if file exists.
  • Use exception handling.

Example 1:

import osfile_path = r'E:\demos\files\sales_2.txt'if os.path.exists(file_path): os.remove(file_path)else: print("The system cannot find the file specified")Code language: Python (python)

Note: Exception handling is recommended over file check because the file could be removed or changed in between. It is the Pythonic way to delete a file that may or may not exist.

Example 2: Exception handling

import osfile_path = r'E:\demos\files\sales_21.txt'try: os.remove(file_path)except: print("The system cannot find the file specified") # your codeCode language: Python (python)

Remove File Using os.unlink() method

If you are using the UNIX operating system use the unlink() method available in the OS module, which is similar to the remove() except that it is more familiar in the UNIX environment.

os.unlink(path, *, dir_fd=None)Code language: Python (python)
  • path – A relative or absolute path for the file object generally in string format.
  • dir_fd – A directory representing the location of the file. The default value is none and this value is ignored in the case of an absolute path.

Let us see the code for deleting the file “profits.txt” which is in the current execution path.

import osos.unlink('profits.txt')Code language: Python (python)

Pathlib Module to Remove File

The pathlib module offers classes representing filesystem paths with semantics appropriate for different operating systems. Thus, whenever we need to work with files in multiple environments, we can use the pathlib module.

The pathlib module was added in Python 3.4. The pathlib.path.unlink() method in the pathlib module is used to remove the file in the mentioned path.

Also, it takes one extra parameter, namely missing_ok=False. If the parameter is set to True, then the pathlib module ignores the File Not Found Error. Otherwise, if the path doesn’t exist, then the FileNotFoundError will be raised.

Let us see the code for deleting the file “profits.txt” which is present in the current execution path.

  • Import a pathlib module
  • Use pathlib.Path() method to set a file path
  • Next, to delete a file call the unlink() method on a given file path.
import pathlib# Setting the path for the filefile = pathlib.Path("profits.txt")# Calling the unlink method on the pathfile.unlink()Code language: Python (python)

Delete all Files from a Directory

Sometimes we want to delete all files from a directory without deleting a directory. Follow the below steps to delete all files from a directory.

  • Get the list of files in a folder using os.listdir(path) function. It returns a list containing the names of the files and folders in the given directory.
  • Iterate over the list using a for loop to access each file one by one
  • Delete each file using the os.remove()

Example:

import ospath = r"E:\demos\files\reports\\"for file_name in os.listdir(path): # construct full file path file = path + file_name if os.path.isfile(file): print('Deleting file:', file) os.remove(file)Code language: Python (python)

Delete an Empty Directory (Folder) using rmdir()

While it is always the case that a directory has some files, sometimes there are empty folders or directories that no longer needed. We can delete them using the rmdir() method available in both the os module and the pathlib module.

Using os.rmdir() method

In order to delete empty folders, we can use the rmdir() function from the os module.

os.rmdir(path, *, dir_fd = None)Code language: Python (python)

The following are the parameters that we need to pass to this method.

  • path – A relative or absolute path for the directory object generally in string format.
  • dir_fd – File directory. The default value is none, and this value is ignored in the case of an absolute path.

Note: In case if the directory is not empty then the OSError will be thrown.

import os# Deleting an empty folderdirectory = r"E:\pynative\old_logs"os.rmdir(directory)print("Deleted '%s' directory successfully" % directory)Code language: Python (python)

Output

Deleted 'E:\pynative\old_logs' directory successfully 

Use pathlib.Path.rmdir()

The rmdir() method in the pathlib module is also used to remove or delete an empty directory.

  • First set the path for the directory
  • Next, call the rmdir() method on that path

Let us see an example for deleting an empty directory called ‘Images’.

import pathlib# Deleting an empty folderempty_dir = r"E:\pynative\old_images"path = pathlib.Path(empty_dir)path.rmdir()print("Deleted '%s' directory successfully" % empty_dir)Code language: Python (python)

Delete a Non-Empty Directory using shutil

Sometimes we need to delete a folder and all files contained in it. Use the rmtree() method of a shutil module to delete a directory and all files from it. See delete a non-empty folder in Python.

The Python shutil module helps perform high-level operations in a file or collection of files like copying or removing content.

shutil.rmtree(path, ignore_errors=False, onerror=None)Code language: Python (python)

Parameters:

  • path – The directory to delete. The symbolic links to a directory are not acceptable.
  • ignore_errors – If this flag is set to true, then the errors due to failed removals will be ignored. If set to true, the error should be handler by the function passed in the one error attribute.

Note: The rmtree() function deletes the specified folder and all its subfolders recursively.

Consider the following example for deleting the folder ‘reports’ that contains image files and pdf files.

import shutil# Deleting an non-empty folderdir_path = r"E:\demos\files\reports"shutil.rmtree(dir_path, ignore_errors=True)print("Deleted '%s' directory successfully" % dir_path)Code language: Python (python)

Output

Deleted 'E:\demos\files\reports' directory successfully 

Get the proper exception message while deleting a non-empty directory

In order to get the proper exception message we can either handle it in a separate function whose name we can pass in the oneerror parameter or by catching it in the try-except block.

import shutil# Function for Exception Handlingdef handler(func, path, exc_info): print("We got the following exception") print(exc_info)# locationdir_path = r'E:\demos\files\reports'# removing directoryshutil.rmtree(dir_path, ignore_errors=False, onerror=handler)Code language: Python (python)

Final code: To delete File or directory

import osimport shutildef delete(path): """path could either be relative or absolute. """ # check if file or directory exists if os.path.isfile(path) or os.path.islink(path): # remove file os.remove(path) elif os.path.isdir(path): # remove directory and all its content shutil.rmtree(path) else: raise ValueError("Path {} is not a file or dir.".format(path))# filedelete(r'E:\demos\files\reports\profits.txt')# directorydelete(r'E:\demos\files\reports')Code language: Python (python)

Deleting Files Matching a Pattern

For example, you want to delete files if a name contains a specific string.

The Python glob module, part of the Python Standard Library, is used tofind the files and folders whose names follow a specific pattern.

glob.glob(pathname, *, recursive=False)Code language: Python (python)

The glob.glob() method returns a list of files or folders that matches the pattern specified in the pathname argument.

This function takes two arguments, namely pathname, and recursive flag ( If set toTrueit will search files recursively in all subfolders)

We can use the wildcard characters for the pattern matching, and the following is the list of the wildcard characters used in the pattern matching.

  • Asterisk (*): Matches zero or more characters
  • Question Mark (?) matches exactly one character
  • We can specify a range of alphanumeric characters inside the[].

Example: Deleting Files with Specific Extension

On certain occasions, we have to delete all the files with a particular extension.

  • Use glob() method to find all text files in a folder
  • Use for loop to iterate all files
  • In each iteration, delete single file.

Let us see few examples to understand how to use this to delete files that match a specific pattern.

Example

import globimport os# Search files with .txt extension in current directorypattern = "*.txt"files = glob.glob(pattern)# deleting the files with txt extensionfor file in files: os.remove(file)Code language: Python (python)

Delete file whose name starts with specific string

import globimport os# Delete file whose name starts with string 'pro'pattern = r"E:\demos\files\reports\pro*"for item in glob.iglob(pattern, recursive=True): os.remove(item)Code language: Python (python)

Delete file whose name contains a specific letters

We can give a range of characters as the search string by enclosing them inside thesquare brackets ([]).

The following example will show how to delete files whose name contains characters between a-g.

import globimport os# search files like abc.txt, abd.txtpattern = r"E:\demos\files_demos\reports\[a-g]*.txt"for item in glob.iglob(pattern, recursive=True): os.remove(item)Code language: Python (python)

Deleting Files Matching a Pattern from all Subfolders

While the glob() function finds files inside a folder, it is possible to search for files inside the subfolders using the iglob() function which is similar to the glob() function.

The iglob() function returns iterator options with the list of files matching a pattern inside the folder and its subfolder.

We need to set the recursive flag to True when we search for the files in subdirectories. After the root folder name, we need to pass ** for searching inside the subdirectories.

import globimport os# Searching pattern inside folders and sub folders recursively# search all jpg filespattern = r"E:\demos\files\reports\**\*.jpg"for item in glob.iglob(pattern, recursive=True): # delete file print("Deleting:", item) os.remove(item)# Uncomment the below code check the remaining files# print(glob.glob(r"E:\demos\files_demos\reports\**\*.*", recursive=True))Code language: Python (python)

Output

Deleting: E:\demos\files\reports\profits.jpgDeleting: E:\demos\files\reports\revenue.jpg

Conclusion

Python provides several modules for removing files and directories.

To delete Files: –

  • Use os.remove() and os.unlink() functions to delete a single file
  • Use pathlib.Path.unlink() to delete a file if you use Python version > 3.4 and application runs on different operating systems.

To delete Directories

  • Use os.rmdir() or pathlib.Path.rmdir() to delete an empty directory
  • use the shutil.rmtree() to recursively delete a directory and all files from it.

Take due care before removing files or directories because all the above functions delete files and folders permanently.

Delete (Remove) Files and Directories in Python (2024)

References

Top Articles
Latest Posts
Article information

Author: Wyatt Volkman LLD

Last Updated:

Views: 6293

Rating: 4.6 / 5 (66 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Wyatt Volkman LLD

Birthday: 1992-02-16

Address: Suite 851 78549 Lubowitz Well, Wardside, TX 98080-8615

Phone: +67618977178100

Job: Manufacturing Director

Hobby: Running, Mountaineering, Inline skating, Writing, Baton twirling, Computer programming, Stone skipping

Introduction: My name is Wyatt Volkman LLD, I am a handsome, rich, comfortable, lively, zealous, graceful, gifted person who loves writing and wants to share my knowledge and understanding with you.