Search code examples
pythonpython-2.7datetime-formatpython-datetime

ValueError: Works with IDLE timeformat error in CMD


This works in IDLE but in Command Prompt Ill get this error "ValueError: time data 'Fri Feb 19 10:00:00 2021' does not match format '%a %b %d %H:%M:%S %Y'"

file_mtime = time.ctime(os.path.getmtime(matfil))

now = datetime.now()
current_time = now.strftime('%Y-%m-%d %H:%M')


file_time_format = '%a %b %d %H:%M:%S %Y'
current_time_format = '%Y-%m-%d %H:%M'

file_tdelta = datetime.strptime(current_time, current_time_format) - datetime.strptime(file_mtime, file_time_format)

print file_tdelta.seconds

if file_tdelta.seconds < 2700:

Solution

  • Ill changed from time.ctime to .strftime and now it works in CMD.

    file_mtime = datetime.fromtimestamp(os.path.getmtime(matfil)).strftime("%Y-%m-%d %H:%M")
    
    now = datetime.now()
    current_time = now.strftime('%Y-%m-%d %H:%M')
    
    timeformat = '%Y-%m-%d %H:%M'
    
    file_tdelta = datetime.strptime(current_time, timeformat) - datetime.strptime(file_mtime, timeformat)
    
    print file_mtime
    print current_time
    print file_tdelta.seconds
    
    #Körs bara om filens datum är nyare än 45 minuter sedan.
    if file_tdelta.seconds < 2700: