Problem with reading python files

Asked

Viewed 554 times

1

What is the difference between, make python open and read a file, to pass the read result between áspas?

For when I open a file, it contains only the word test and I pass this reading result by a hash algorithm the result is:

cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e

Note: Result is not expected

Now step as parameter within code(not user input) for the string hash algorithm "test"

b123e9e19d217169b981a61188920f9d28638709a5132201684d792b9264271b7f09157ed4321b1c097f7a4abecfc0977d40a7ee599c845883bd1074ca23c4af

Note: This is the expected result; I’m using the .Encode('utf-8') in charge

Commando:

hashlib.sha512(hash_target).hexdigest()

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

To read the file I used the command:

archive = open(file, 'r')
  • 1

    How did you read the file?

  • And there just happened to be no one left \n at the end of the file read line?

  • I did the test with the ' n' going through the algorithm, together with the "test" (as I did in the second attempt, also shown in the question), ie "test n", and gave a result different from the first shown in the question, ie theoretically not.

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.

Behold:

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

type(str1)
_io.TextIOWrapper

type(str2)
str

str1==str2
False 

Now look at it this way:

string1 = open(file,'r').readlines()[0].rstrip()
string2 = 'teste'
string1==string2
True

Browser other questions tagged

You are not signed in. Login or sign up in order to post.