Python Forum
Compare current date on calendar with date format file name
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare current date on calendar with date format file name
#1
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.
Reply
#2
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'))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Date Time Series Help...Please spra8560 2 380 Feb-01-2024, 01:38 PM
Last Post: spra8560
  Create dual folder on different path/drive based on the date agmoraojr 2 458 Jan-21-2024, 10:02 AM
Last Post: snippsat
  Python date format changes to date & time 1418 4 623 Jan-20-2024, 04:45 AM
Last Post: 1418
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 1,057 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  Formatting a date time string read from a csv file DosAtPython 5 1,300 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  PDF properties doesn't show created or modified date Pedroski55 4 1,101 Jun-19-2023, 08:09 AM
Last Post: Pedroski55
  How should I run pip-date in python3? newbieAuggie2019 5 1,900 Mar-31-2023, 03:21 PM
Last Post: snippsat
  How to see the date of installation of python modules. newbieAuggie2019 4 1,640 Mar-31-2023, 12:40 PM
Last Post: newbieAuggie2019
  Review my code: convert a HTTP date header to a datetime object stevendaprano 1 2,009 Dec-17-2022, 12:24 AM
Last Post: snippsat
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,134 Dec-15-2022, 04:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020