In this article we will discuss how to delete few files from a directory based on matching pattern or wildcard.

Suppose we have a directory that contains some log files and some text files and we want to delete all .txt files from that directory i.e. files that ends with string “.txt”.
Let’s discuss how to do that using different techniques,

Remove files by pattern using glob.glob() & os.remove()

To remove files by matching pattern, we need to get list of all files paths that matches the specified pattern using glob.glob() and then delete them one by one using os.remove() i.e.

It will remove all ‘.txt’ files in directory /home/varung/Documents/python/logs/ but it will not remove files in it’s sub directories.
Let’s understand how it works,

Get list of files using glob.glob()

glob.glob() accepts path name as shell pattern and finds the path of all the files that matches the specified pattern. As by default recursive parameter is False, therefore it will find files of matching pattern in given directory only not the sub directories.

Then Iterate over the file path list and delete each file using os.remove(), also catch any exception that can happen due to file permissions.

As we have seen by this approach we can not recursively delete files from sub directories. For that we need another solution,

Recursively Remove files by matching pattern or wildcard

In glob.glob() To recursively find the files that matches the given pattern we need to pass recursive parameter as True & also use “**” in matching pattern i.e.

It Will recursively search all the ‘txt’ files including files in sub directories. Then we can iterate over the list and delete each file on by one using os.remove() i.e.

It will delete all the txt files from /home/varung/Documents/python/logs/ and it’s sub directories.

Recursively Remove files by matching pattern or wildcard using os.walk()

os.walk() generates filename in given directory by walking over the tree structure in top down or bottom up approach i.e.

For each directory and it’s sub directory it yields a tuple (rootDir, subdirs, filenames) i.e.

  • rootDir
    • path of the directory it’s iterating
  • subdirs
    • List of all the sub directories inside this root dir.
  • filenames
    • List of all names of files in root dir

It will iterate over all the sub directories in specified directory and in each iteration sub directory will become root dir.

Let’s use this os.walk() to get a list of all files in given directory that matches pattern. Then delete those files i.e.

It will delete all the ‘*.txt’ files from directory /home/varung/Documents/python/logs and also from it’s sub directories.

Let’s create a Generic function to delete all the files from a given directory based on matching pattern and it will also return the files names that were not deleted due to some error.

Let’s call this function to delete files based on matching pattern i.e.

Complete example is as follows,


Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.