Home > Enterprise >  Why glob is not getting files in subfolders
Why glob is not getting files in subfolders

Time:10-20

I have following directory structure

$ find
.
./file1.html
./soquest_glob.py

./dir1
./dir1/file2.html

./dir2
./dir2/file3.html

(I have added blank lines above to clarify files in different folders).

I am trying to find all html files (including those in subfolders) with following Python code using glob package:

$ cat soquest_glob.py

import glob
flist = glob.glob("*.html", recursive=True)
print(flist)

However, when I run this code, it finds only file in current folder, not in subfolders:

$ python3  soquest_glob.py
['file1.html']

Where is the problem and how can it be solved?

CodePudding user response:

The recursive argument to glob.glob effects the behavior of **. You need to use a pattern like: glob.glob("**/*.html", recursive=True).

  • Related