Selecting multiple files according to timestamp with glob

Question:

I have a directory with 7000 daily .tif files, spanning from 2002 to 2022. Each file has a respective indication of its timestamp, in the yyyy/mm/dd format, as: My_file_20020513.tif

I’m trying to select the file paths according to their respective month, i.e. all files from January, all files from February, and so on.

I thought using a simple wildcard showing my month of interest would solve the problem, such as:

january_files = glob.glob('My_folder/My_file_*01*.tif')

But this ended up selecting all files that have a number 01 on its string, such as all the first days of a month, and even October’s days that starts with a 1.

Is there a way I can use glob for selecting the file paths for a specific month only?

Asked By: Gabriel Lucas

||

Answers:

Use ? to match a single character. Then you can use four ? to match any year.

january_files = glob.glob('My_folder/My_file_????01*.tif')
Answered By: Barmar
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.