Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ValueError: time data 'Mon Mar 31 14:52:16 2014' does not match format "%d/%m/%Y"

I am trying to check the last modified date time of a file but can’t compare the date because of its format :

BPath = r'Path\\Path\\'

for root, dirs, files in os.walk(BPath):
    for name in files:
        if name.endswith((".xlsm")):
            DateModification = time.ctime(os.path.getmtime(BPath+str(name)))
            DateModification = time.strptime(DateModification, "%d/%m/%Y")
            TargetDate=time.strptime("01/01/2022", "%d/%m/%Y")
            
            if(DateModification>TargetDate):
                print(DateModification)

Struggling with the following error :

ValueError: time data 'Mon Mar 31 14:52:16 2014' does not match format "%d/%m/%Y"

What does this mean ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

time.ctime returns a string of the format 'Mon Mar 31 14:52:16 2014' while the format you provide to strptime is "%d/%m/%Y" – these do not match.

Use datetime.datetime.fromtimestamp(your_mtime).strftime("%d/%m/%Y") instead of time.ctime.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading