Click here to Skip to main content
15,355,659 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello. So I was just messing around with reading text files in Python. I decided to read a spesific line of the text file, and use if else statement. Unfortunately, the result isn't my expectation. Here is my code:

file = open('testing.txt')
lines = file.readlines()
errorinfo = lines[0]

if errorinfo == "A":
    print("Yes you are A!")

else:
    print("No you're not A!")


And the text file content:

A
B
C


The code still detects as "not A".

What I have tried:

The solution I have tried is to write it without a variable (writing it directly in the if else statement)
Posted
Updated 17-Mar-22 3:40am

1 solution

Add the following after the call to file.readlines() and you will see why:
Python
print(lines)
   
Comments
TheStupidDev 17-Mar-22 10:56am
   
Thank you so much! I finally realized that I forgot the /n! Thanks again!
Richard MacCutchan 17-Mar-22 11:05am
   
That is why during development, you should make use of extra print statements or use the debugger, so you can see what is happening.
TheStupidDev 18-Mar-22 2:41am
   
You can call me a rookie if I must say. Thanks for the tip, helps a lot!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900