-3

I have a series of filenames which I need to process and they have year and Julian day in their filename, e.g. A1998237.tif.

How can I group these files based on their names, by month (e.g. Jan, Feb ...)?

This is a pre-procedure and after this I will read this files into memory. Then I will take the average of matrices associated with each of this files to produce monthly matrices.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
FlyingMGET
  • 5
  • 1
  • 8

1 Answers1

1

Parse out the julian day, file each filename into a dictionary for that month:

from datetime import datetime
import os.path

# months from 0 to 11
files_by_month = [[] for _ in range(12)]

for filename in filenames:
    dt = datetime.strptime(os.path.splitext(filename)[0][1:], '%Y%j')
    files_by_month[dt.month - 1].append(filename)

files_by_month uses 0-based indexing to store a list per month; January is 0, etc.

This assumes that the filename always starts with one letter, followed by the year + julian year day, followed by the extension. If these assumptions are incorrect you need to provide more information on what sort of patterns there are in your filenames.

Demo for your sample filename:

>>> from datetime import datetime
>>> import os.path
>>> filename = 'A1998237.tif'
>>> datetime.strptime(os.path.splitext(filename)[0][1:], '%Y%j')
datetime.datetime(1998, 8, 25, 0, 0)

which would be filed in the 'august' bucket, files_by_month[7].

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks for the respond. It took me a while to get this to run properly. I had to change datetime.strptime to time.strptime. Looked it up from this [post](http://stackoverflow.com/questions/12070193/why-is-datetime-strptime-not-working-in-this-simple-example) – FlyingMGET Mar 30 '14 at 20:17
  • How can I preserve the path after the grouping? – FlyingMGET Mar 30 '14 at 20:47
  • insert `os.path.join(directoryname, filename)`; [`os.listdir()` lists relative names only](http://stackoverflow.com/questions/15535188/no-such-file-or-directory-while-trying-to-read-files-in-a-directory-python). – Martijn Pieters Mar 30 '14 at 21:30