Good afternoon.
I'm new to the world of Python, I don't have any knowledge, but I have a challenge.
I have some images in a folder, where the name of these images are in a date format.
Example:
Image: xxxxx
Image file name: 25032024 i.e. 03/25/2024.
I need to create a program where I look at the current date on my calendar and compare it to the name of the image file.
If the date is the same, I would like to take this image and send it to a specific Teams group.
You should read about the Python module datetime.
Here is a good start
You can get the dates from your file names and today's date like this:
from datetime import date, time, datetime
filename = '25032024.jpg'
# get today's date in the same format as the file name
today = date.today() # returns datetime.date(2024, 3, 26)
date2day = today.strftime("%d%m%Y") # returns '26032024'
# split the file name on . to get the date
# this returns a list
# get the first elemant of the list
data = filename.split('.')
fdate = data[0] # looks like '25032024'
# get the date as a datetime object
# need to tell datetime the format and order of the date: %d = day, %m = month, %Y = year
dt = datetime.strptime(fdate, "%d%m%Y") # returns datetime.datetime(2024, 3, 25, 0, 0)
# get the date of the file as day month, year
date_of_file = dt.strftime("%d%m%Y") # returns '25032024'
# compare the dates
date_of_file == date2day # returns False
# try another file date
fdate2 = '26032024'
dt2 = datetime.strptime(fdate2, "%d%m%Y") # returns datetime.datetime(2024, 3, 26, 0, 0)
date_of_file2 = dt.strftime("%d%m%Y") # returns '26032024'
date_of_file2 == date2day # returns True
First you need a list of all files. Loop through the list, getting each date from the file name and comparing it with today as above.
datetime has many abbreviations to help you deal with different date and time formats. Below are some of them:
dt = datetime.now()
print(dt)
print('\nDirectives\n--------------')
print(dt.strftime('Weekday short version : %a'))
print(dt.strftime('Weekday full version : %A'))
print(dt.strftime('Weekday as a number : %w'))
print(dt.strftime('Day of month : %d'))
print(dt.strftime('Month Name short ver : %d'))
print(dt.strftime('Month Name full ver : %b'))
print(dt.strftime('Month as a number : %m'))
print(dt.strftime('Year short version : %y'))
print(dt.strftime('Year full version : %Y'))
print(dt.strftime('Hour (00-23) : %H'))
print(dt.strftime('Hour (00-11) : %I'))
print(dt.strftime('AM/PM : %p'))
print(dt.strftime('Minute : %M'))
print(dt.strftime('Second : %S'))