Python if folder exists delete

Delete (Remove) Files and Directories in Python

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.

Before deleting a file

After deleting a file

Understand the os.remove() method

os.remove(path, *, dir_fd = None)

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:

import os file_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")

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 os file_path = r'E:\demos\files\sales_21.txt' try: os.remove(file_path) except: print("The system cannot find the file specified") # your code

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.

  • 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.
Читайте также:  Mysqli store result php

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

import os os.unlink('profits.txt')

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 file file = pathlib.Path("profits.txt") # Calling the unlink method on the path file.unlink()

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()
import os path = 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)

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)

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 folder directory = r"E:\pynative\old_logs" os.rmdir(directory) print("Deleted '%s' directory successfully" % directory)
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.

Читайте также:  Python pil image pixels

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

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

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)
  • 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 folder dir_path = r"E:\demos\files\reports" shutil.rmtree(dir_path, ignore_errors=True) print("Deleted '%s' directory successfully" % dir_path)
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 Handling def handler(func, path, exc_info): print("We got the following exception") print(exc_info) # location dir_path = r'E:\demos\files\reports' # removing directory shutil.rmtree(dir_path, ignore_errors=False, onerror=handler)

Final code: To delete File or directory

import os import shutil def 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)) # file delete(r'E:\demos\files\reports\profits.txt') # directory delete(r'E:\demos\files\reports')

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 to find the files and folders whose names follow a specific pattern.

glob.glob(pathname, *, recursive=False)

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 to True it 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.
Читайте также:  TextEdit Demo

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

import glob import os # Search files with .txt extension in current directory pattern = "*.txt" files = glob.glob(pattern) # deleting the files with txt extension for file in files: os.remove(file)

Delete file whose name starts with specific string

import glob import 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)

Delete file whose name contains a specific letters

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

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

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

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 glob import os # Searching pattern inside folders and sub folders recursively # search all jpg files pattern = 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)) 
Deleting: E:\demos\files\reports\profits.jpg Deleting: 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.

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

About Vishal

I’m Vishal Hule, Founder of PYnative.com. I am a Python developer, and I love to write articles to help students, developers, and learners. Follow me on Twitter

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ

Источник

Оцените статью