Home > Mobile >  Selecting multiple files according to timestamp with glob
Selecting multiple files according to timestamp with glob

Time:08-16

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?

CodePudding user response:

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')
  • Related