Supposing we have a folder structure like the below:
root/a/1.txt
root/a/2.txt
root/a/3.json
root/b/1.txt
root/b/2.txt
root/b/3.json
root/c/1.txt
root/c/2.txt
And I wanna iterate files in some special subdirectories(not all). For example, get all .txt
files in subdirectories a
and b
.
Of course, we can easily achieve it with for
loop and os.listdir()/glob.glob()
in python. But I am looking for a python package (if exists) that behave as simple as unix shell ls
.
ls ./root/{a,b}/*.txt
# output:
# root/a/1.txt
# root/a/2.txt
# root/b/1.txt
# root/b/2.txt
Does anyone know such a python package?
Technically you can use glob for that example as a
and b
are single characters.
>>> glob.glob('./root/[ab]/*.txt')
['./root/a/1.txt', './root/a/2.txt', './root/b/1.txt', './root/b/2.txt']
Otherwise you'd need multiple glob calls (which you could make a function for)
glob('./root/dir1/*.txt') + glob('./root/dir1/*.txt')
However you're asking about {a,b}
which is "shell brace expansion" and not part of the glob syntax.
See https://pypi.org/project/wcmatch/ for that and more.