Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Selecting multiple files according to timestamp with glob

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading