Reputation: 29
I wrote a script that retrieves a date from a textfile, converts that to a datetime
and checks if the current time is later than the datetime in the file. I wrote the following code for that:
from datetime import datetime
f = open("token.txt", "r")
expiry_date = f.readline()
f.close()
if datetime.now() >= datetime.strptime(expiry_date, "%Y-%m-%d %H:%M:%S.%f"):
#DO STUFF
However, I get the following error:
ValueError: unconverted data remains:
Anyone knows where I went wrong and how I can fix this?
The line I want to retrieve from the textfile contains a date formatted like this:
2020-05-10 19:29:51.503962
Upvotes: 0
Views: 35
Reputation: 11134
When you call readline()
, there is a \n
appended to the line. strip the newline first.
Please try:
if datetime.now() >= datetime.strptime(expiry_date.strip(), "%Y-%m-%d %H:%M:%S.%f"):
#DO STUFF
It will work.
Upvotes: 2