-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-dates.py
More file actions
31 lines (26 loc) · 1.19 KB
/
get-dates.py
File metadata and controls
31 lines (26 loc) · 1.19 KB
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
import datetime
import re
def extract_unique_days(file_path):
unique_dates = set() # Use a set to store unique dates
date_pattern = re.compile(r'\d{4}-\d{2}-\d{2}') # Regex to match YYYY-MM-DD format
with open(file_path, 'r') as file:
for line in file:
file_name = line.strip().split('\\')[-1] # Get the file name
# Search for date pattern in the file name
match = date_pattern.search(file_name)
if match:
date_part = match.group(0) # Extract the date string
try:
date_obj = datetime.datetime.strptime(date_part, '%Y-%m-%d')
# Format date as 'd MMM yyyy'
formatted_date = date_obj.strftime('%d %b %Y').lstrip('0')
unique_dates.add(formatted_date)
except ValueError as e:
print(f"Skipping file due to error parsing date: {file_name}, Error: {str(e)}")
else:
print(f"No valid date found in file name: {file_name}")
return list(unique_dates)
# Example usage
file_path = 'D:\\My Photos\\corrupted_photos.txt'
dates = extract_unique_days(file_path)
print(dates)