Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ghboard2010

macrumors regular
Original poster
Aug 7, 2010
169
97
Somewhere
Code:
import os
PassWorD = open('password')
print(PassWorD, 'is type: ', type(PassWorD))
passWord = PassWorD.read()
print(passWord, 'is type: ',type(passWord))
secretPassword = input("enter your password: ")
print(secretPassword, 'is type: ', type(secretPassword))
if passWord == secretPassword:
    print("true")
else:
    print("false")
   
PassWorD.close()
<_io.TextIOWrapper name='password' mode='r' encoding='UTF-8'> is type:  <class '_io.TextIOWrapper'>
foo

 is type:  <class 'str'>
enter your password:  foo

foo is type:  <class 'str'>
false
So, I am beginning with Python. I banged up this little piece to view file opening, and data input from the keyboard, and now I'm scratching' my head, WTFO?!
The conditional check should be testing the value of two objects of the string class. PassWord is converted to a string class object by assignment to passWord, secretPassword is a string class object. Both appear to contain the string 'foo', yet the conditional test yields a false response. WTFO? Can any pythonistas interpret this result?
 
Without seeing anything else, my first guess is that one or the other has a newline, or possibly a CR-LF, at the end of it, while the other does not.

A simple check is to print their lengths. If they both aren't 3, then you've got extra characters.


EDIT: After I look more closely at what you posted for the output:
Code:
foo

 is type:  <class 'str'>
it looks like a couple of newlines in there. Notice it's markedly different from this output:
Code:
foo is type:  <class 'str'>
I see no extra newlines here.
 
Last edited:
Yeah, I was thinking about that. I opened the source file password.txt., 'lo and behold there was indeed two extra blank lines in the file. Removing these yielded the this:

Code:
import os
PassWorD = open('password')
print(PassWorD, 'is type: ', type(PassWorD))
passWord = PassWorD.read()
print(passWord, 'is type: ',type(passWord), (len(passWord)))
#print(len(passWord))
secretPassword = input("enter your password: ")
print(secretPassword, 'is type: ', type(secretPassword), (len(secretPassword)))
if passWord == secretPassword:
    print("true")
else:
    print("false")
   
PassWorD.close()
<_io.TextIOWrapper name='password' mode='r' encoding='UTF-8'> is type:  <class '_io.TextIOWrapper'>
foo is type:  <class 'str'> 3
enter your password:  foo
foo is type:  <class 'str'> 3
true

The expected result for a match. So, removing the extra blank lines was one solution. I think I'll look at parsing out the blanks and then compare that which remains. . .:) Thank You, chown, good call.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.