-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_path_matcher_with_date.py
58 lines (40 loc) · 1.62 KB
/
file_path_matcher_with_date.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import re
from collections import namedtuple
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
class FilePathMatcherWithDate:
def __init__(self, data_folder_path) -> None:
self.month = None
self.year = None
self.data_folder_path = data_folder_path
def setDate(self, date):
splitted_date = date.split("/") # date = yyyy/m
if len(splitted_date) > 1:
self.month = splitted_date[1]
self.year = splitted_date[0]
def get_files_path(self):
if self.month and self.year:
return ""
elif self.year:
return self.get_specific_year_file_paths()
else:
print("Date must be Required")
def get_specific_year_file_paths(self):
file_paths_of_a_year = []
for file_name in os.listdir(self.data_folder_path):
file_year = self.get_file_month_year(file_name).year
if self.year == file_year:
file_paths_of_a_year.append(
os.path.join(self.data_folder_path, file_name))
return file_paths_of_a_year
def get_file_month_year(self, filename):
matched_result = list(re.finditer(
r'(?P<year>\d+)_(?P<month>\w+)', filename))
DateObj = namedtuple("DateObj", ['month', 'year'], defaults=['', ''])
if len(matched_result) > 0:
matched_object = matched_result[0].groupdict()
date_object = DateObj(
matched_object['month'], matched_object['year'])
return date_object
return DateObj()