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

Time:03-29

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 ?

CodePudding user response:

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.

  • Related