SoFunction
Updated on 2024-11-13

python implementation of deleting files and folders in a specified directory

In our daily office, we can use Python to batch delete files and folders, but be sure to pay attention to the irreversibility of this process, so be sure to make a backup before the operation.

Below we describe two methods for performing traversal, filtering and deletion. The main modules used are the os module and the pathlib module. For deleting files we use () and for deleting directories we use ()

i. os deletion method

We can use the os module listdir() to traverse the files under the directory, if it is the current directory is specified with path =, and then use shutil to delete, very simple, practical and convenient.

First understand a few key methods:

  • ()This function returns the current directory.
files = () The current execution directory is stored in # files.
  • ()This function breaks down the extension of a filename.
  • (file_path)This function lists all files and folders under a given path.
  • (file_path, file_name)This function concatenates the path with the filename.
  • (path)This function determines whether the specified file path is a file.
  • (path)This function determines if the specified path is a folder.
  • (filename)This function deletes files at the specified path.

The algorithm process is, first constructor, check whether the specified directory is empty or not, if not, use OS and iterative deletion method to delete all directories and files under the test directory, the code is as follows:

import os
import shutil
def  del_file(path):
      if not (path):
            print('The catalog is empty!')
      else:
            for i in (path):
                  path_file = (path,i)  # Fetch the absolute path of a file
                  print(path_file)
                  if (path_file):
                        (path_file)
                  else:
                        del_file(path_file)
                        (path_file)
if __name__ == '__main__':
      path=r'test' 
      del_file(path)
    

II. Pathlib deletion method

pathlib is a more powerful module than os. It can perform traversal, deletion and many other commands.

Using pathlib, shutil, deletion is faster. unlink() deletes files, rmtree() deletes directories, all in one go.

import shutil
from pathlib import Path
def  del_file(path):
      for elm in Path(path).glob('*'):
            print(elm)
            () if elm.is_file() else (elm)
if __name__ == '__main__':
      path=r'test' 
      del_file(path)

III. Post-learning reflections

1. pathlib has a lot of good methods, such as unlink, glob, replace, etc., flexible use can solve a number of problems.

2. If you are not familiar with pathlib, you can learn os well enough to meet your daily needs.

3. The use of the process can use the list of derivatives, and python in the ternary operator a line of code to get it done, you can further optimize the code.

To this point this article on python to delete the specified directory files and folders to achieve the article is introduced to this, more related python to delete the specified directory file content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!