pathlib.Path().glob() and multiple file extension

A bit late to the party with a couple of single-line suggestions that don’t require writing a custom function nor the use of a loop and work on Linux:

pathlib.Path.glob() takes interleaved symbols in brackets. For the case of “.txt” and “.xls” suffixes, one could write

files = pathlib.Path('temp_dir').glob('*.[tx][xl][ts]')

If you need to search for “.xlsx” as well, just append the wildcard “*” after the last closing bracket.

files = pathlib.Path('temp_dir').glob('*.[tx][xl][ts]*')

A thing to keep in mind is that the wildcard at the end will be catching not only the “x”, but any trailing characters after the last “t” or “s”.

Prepending the search pattern with “**/” will do the recursive search as discussed in previous answers.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)