Well the program is supposed to check if the it sees an "ok" next to the email and if it does spit out the email in another txt file.
Input txt file:
[email protected],ok
[email protected],fail
[email protected],fail
[email protected],fail
[email protected],ok
[email protected],fail
[email protected],fail
[email protected],ok
python code is:
readEmails = open("C:\\Users\\Greg\\Desktop\\rewriteEmails.txt","r")
writeEmails = open("C:\\Users\\Greg\\Desktop\\OutputEmails.txt","w")
for line in readEmails:
subbed = line[-3:]
if subbed == "ok":
split = line.split(",")
writeEmails.write(split[0])
print("Email Added")
else:
print("Error")
The program seems to always go to the else part of the if statement. I'm kinda having a brain fart; i would love your advice.
Your problem is this line
subbed = line[-3:]
This returns the following:
ok\n
or
il\n
Thus, your if
statement checking if subbed == 'ok'
will fail every time.
You can fix this by only taking the last two characters, after stripping the newline:
subbed = line.strip()[-2:]
My original answer didn't account for reading from a file. That answer assumed that the results would be either ,ok
or ail
. This is not the correct response when reading from a file. The answer above has been modified to show what will be returned and adjusts the solution appropriately.