Wednesday, November 2, 2022

[FIXED] Why glob is not getting files in subfolders

Issue

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?


Solution

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



Answered By - DanielB
Answer Checked By - David Goodson (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.