This is an abstraction of a more complex script. The goal is to get information from unread emails and add it to a CSV file. It also needs to check whether the file exists in the first place. To simplify I put print statements instead of function calls. This still brings up the issue.
import ezgmail
import csv
orderEmails = ezgmail.search('subject:Automatic message, label:UNREAD')
unreadThreads = ezgmail.unread()
amountMessages = len(orderEmails[0].messages)
number: int
for number in range(amountMessages):
message = orderEmails[0].messages[number]
date = message.timestamp
try:
with open('file.csv','r') as file:
csvReader = csv.reader(file, delimiter=',')
for row in csvReader:
if row[0] == date:
print("Timestamp already exists")
else:
print("Ok")
except FileNotFoundError:
print("No such file")
Contents of file.csv
Date,Name,City
2020-12-27 18:11:16,John,New York
2020-12-29 17:44:23,Mary,Berlin
The variable "date" should be this string in the first run of the loop:
2020-12-27 18:11:16
Then this one:
2020-12-30 12:51:32
What I expected to get is:
Timestamp already exists
Ok
What I get instead is:
Ok
Ok
Ok
Ok
Ok
Ok
Besides obviously not understanding why this happens, I also don't understand why 6 "Ok" messages.
question from:https://stackoverflow.com/questions/65849116/comparing-two-strings-in-python-not-showing-the-intended-result