Problem with reading files in python

1

What is the difference between making python open and reading a file, passing the reading result between blades?

When I open a file that contains only the word test and I pass this result from the read by a hash algorithm the result is:

  

cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e

Note: Result is not as expected

Now if I pass as a parameter inside the code (it is not user input) to the hash algorithm the string "test"

  

b123e9e19d217169b981a61188920f9d28638709a5132201684d792b9264271b7f09157ed4321b1c097f7a4abecfc0977d40a7ee599c845883bd1074ca23c4af

Note: This is the expected result; I'm using .encode ('utf-8') in the command

Command:

hashlib.sha512(hash_target).hexdigest()

hash_target is the string that goes through the hash algorithm, which in the example above would be the test

To read the file I used the command:

archive = open(file, 'r')
    
asked by anonymous 09.09.2017 / 02:54

1 answer

1

After reading the file you need to extract the contents of it (with readlines, for example), before that the variable is a text.IOwrapper and not a string.

See:

str1 = open(file, 'r')
str2 = 'teste'

type(str1)
_io.TextIOWrapper

type(str2)
str

str1==str2
False 

Now look at this another way:

string1 = open(file,'r').readlines()[0].rstrip()
string2 = 'teste'
string1==string2
True
    
09.09.2017 / 14:58