

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use Glob() function to find files recursively in Python?
To use Glob() to find files recursively, you need Python 3.5+. The glob module supports the "**" directive(which is parsed only if you pass recursive flag) which tells python to look recursively in the directories.
example
import glob for filename in glob.iglob('src/**/*', recursive=True): print(filename)
You can check the filename using whatever condition you want using an if statement. For older Python versions, you can use os.walk to recursively walk the directory and search the files.
example
import os, re, os.path pattern = "^your_regex_here$" mypath = "my_folder" for root, dirs, files in os.walk(mypath): for file in filter(lambda x: re.match(pattern, x), files): print(file)
This will match the file name to the regex you specify and print their names.
- Related Questions & Answers
- How to rename multiple files recursively using Python?
- How to touch all the files recursively using Python?
- glob() function in PHP
- List files recursively in C#
- How to use chmod recursively on Linux?
- Java program to delete all the files in a directory recursively (only files)
- How to Recursively Search all Files for Strings on a Linux
- Java program to List all files in a directory recursively
- How to find difference between 2 files in Python?
- How to use chrome webdriver in Selenium to download files in Python?
- How to use Lambda Function in Python?
- PHP glob://
- How to use .svg files in a webpage?
- How to create a directory recursively using Python?
- How to remove a directory recursively using Python?
Advertisements