September 2021
Intermediate to advanced
152 pages
3h 5m
English
| Tip 28 | glob() the Files |
★2.7, 3.4+ Anyone who works with the shell command line knows how easy it is to get a listing of all files or the files whose names match some pattern. For example, this is how you get the names of all files that contain seven characters and the extension pml:
| | /home/dzpythonic> ls ???????.pml |
| | Changes.pml General.pml Preface.pml |
You wish it were equally easy in Python. As a matter of fact, it is, thanks to the module glob. The namesake function takes the pattern as a string and returns a list of all matching filenames. You can use the “standard” shell-style wildcards like ? (matches any single character) and * (matches any number of characters):
| | glob.glob("???????.pml") |
| => | ['Changes.pml', 'General.pml', ... |
Read now
Unlock full access