{"id":7343,"date":"2021-05-28T17:57:51","date_gmt":"2021-05-28T12:27:51","guid":{"rendered":"https:\/\/python-programs.com\/?p=7343"},"modified":"2021-11-22T18:40:44","modified_gmt":"2021-11-22T13:10:44","slug":"python-how-to-remove-files-by-matching-pattern-wildcards-certain-extensions-only","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-remove-files-by-matching-pattern-wildcards-certain-extensions-only\/","title":{"rendered":"Python: How to remove files by matching pattern | wildcards | certain extensions only?"},"content":{"rendered":"
In this ultimate tutorial, we are going to discuss how to remove files from a directory based on a matching pattern or wildcard, or specific extensions.<\/p>\n
Let\u2019s discuss how to delete text files using different techniques, 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.<\/p>\n
Then, continue your read so that you can successfully learn to remove files by matching patterns or wildcards by the following methods and techniques.<\/p>\n
First, we will get a list of all file paths that match the specified patterns using glob.glob() and then delete all text files.<\/p>\n
import os\r\nimport glob\r\n# Get a list of all the file paths that ends with .txt from in specified directory\r\nfileList = glob.glob('C:\/\/Users\/HP\/Desktop\/A plus topper\/*.txt')\r\n# Iterate over the list of filepaths & remove each file.\r\nfor filePath in fileList:\r\n try:\r\n os.remove(filePath)\r\n except:\r\n print(\"Error while deleting file : \", filePath)<\/pre>\nSo you can see that it will remove all ‘.txt’ files in the directory ‘C:\\\\Users\\HP\\Desktop\\A plus topper\\*.txt’. It will remove all text files because we mention” *.txt “.<\/p>\n
<\/a>Get the list of files using glob.glob()<\/h3>\n
glob.glob() accepts path name and finds the path of all the files that match the specified pattern. By default recursive parameter is False, which means that it will find files in the main directory, not in a subdirectory.<\/p>\n
glob.glob(pathname, *, recursive=False)<\/code><\/p>\n
As we have seen by this approach we can not recursively delete files from subdirectories. For that, we will find another solution,<\/p>\n
Read More:<\/span><\/p>\n
\n
- Python: How to convert a timestamp string to a datetime object using datetime.strptime()<\/a><\/li>\n
- How to get first key in Dictionary \u2013 Python<\/a><\/li>\n<\/ul>\n
<\/a>Recursively Remove files by matching pattern or wildcard<\/h3>\n
It will search all the \u2018txt\u2019 files including files in subdirectories because we will use
'C:\/\/Users\/HP\/Desktop\/A plus topper\/**\/*.txt'<\/code>‘ **\u00a0 ‘ in it.<\/p>\n
Then we can iterate over the list and delete each file one by one using os.remove().<\/p>\n
import os\r\nimport glob\r\n# get a recursive list of file paths that matches pattern including sub directories\r\nfileList = glob.glob('C:\/\/Users\/HP\/Desktop\/A plus topper\/**\/*.txt', recursive=True)\r\n# Iterate over the list of filepaths & remove each file.\r\nfor filePath in fileList:\r\n try:\r\n os.remove(filePath)\r\n except OSError:\r\n print(\"Error while deleting file\")<\/pre>\nIt will delete all the text files from the directory and its sub-directories.<\/p>\n
<\/a>Recursively Remove files by matching pattern or wildcard using os.walk()<\/h3>\n
In this, we are going to use os.walk(). It generates filename in the given directory by walking over the tree structure in a top-down or bottom-up approach.<\/p>\n
os.walk(top, topdown=True, onerror=None, followlinks=False)<\/code><\/p>\n
It will return a tuple consisting of the main directory, a list of all subdirectories, and a list of all file names in the main directory.<\/p>\n
Let\u2019s use this os.walk() to get a list of all files in a given directory that matches a pattern. Then delete those files,<\/p>\n
import os\r\nimport fnmatch\r\n# Get a list of all files in directory\r\nfor rootDir, subdirs, filenames in os.walk('C:\/\/HP\/Users\/Desktop\/A plus topper'):\r\n # Find the files that matches the given patterm\r\n for filename in fnmatch.filter(filenames, '*.txt'):\r\n try:\r\n os.remove(os.path.join(rootDir, filename))\r\n except OSError:\r\n print(\"Error while deleting file\")<\/pre>\nIt will delete all the text files from the directory and also from its subdirectories.<\/p>\n
Now we are going to create a Generic function to delete all the files from a given directory based on a matching pattern and it will also return the names of the files that were not deleted due to some error.<\/p>\n
import os\r\nimport fnmatch\r\n'''\r\nGeneric function to delete all the files from a given directory based on matching pattern\r\n'''\r\ndef removeFilesByMatchingPattern(dirPath, pattern):\r\n listOfFilesWithError = []\r\n for parentDir, dirnames, filenames in os.walk(dirPath):\r\n for filename in fnmatch.filter(filenames, pattern):\r\n try:\r\n os.remove(os.path.join(parentDir, filename))\r\n except:\r\n print(\"Error while deleting file : \", os.path.join(parentDir, filename))\r\n listOfFilesWithError.append(os.path.join(parentDir, filename))\r\n return listOfFilesWithError\r\nlistOfErrors = removeFilesByMatchingPattern('\/home\/varung\/Documents\/python\/logs\/', '*.txt')\r\nprint('Files that can not be deleted : ')\r\nfor filePath in listOfErrors:\r\n print(filePath)<\/pre>\nSo in the above code, you can see that it will also return file names that can not be deleted.<\/p>\n
Conclusion:<\/h3>\n
In this article, we have seen how to remove files from a directory based on matching patterns or wildcards, or certain extensions.<\/p>\n","protected":false},"excerpt":{"rendered":"
In this ultimate tutorial, we are going to discuss how to remove files from a directory based on a matching pattern or wildcard, or specific extensions. How to delete text files using different techniques? Remove files by pattern using glob.glob() & os.remove() Get the list of files using glob.glob() Recursively Remove files by matching pattern …<\/p>\n