is there a way to print the output of this function without calling it again?

Viewed 42

this script hashes a file, but i can't get it to print out the result without calling the function again (the is called in another file and if i call the function hash_file() the other code starts messing)

import hashlib
   def hash_file():
         # make a hash object
         h = hashlib.sha1()
         filename = input('insert the path of the file:  ')
         # open file for reading in binary mode
         with open(filename,'rb') as file:
     
             # loop till the end of the file
             chunk = 0
             while chunk != b'':
                 # read only 1024 bytes at a time
                 chunk = file.read(1024)
                 h.update(chunk)
     
         # return the hex representation of digest
         return h.hexdigest()

as this the bot doesn't print any output and i can't find a way to print it, i tried with

print(h.hexdigest())but it doesn't print anything,

and i can't think of any other way can you please help me?

1 Answers

h doesn't exist outside the function. You need to save the return value of hash_file (which is h.hexdigest()): v = hash_file(); print(v)

Related