0

I have a list of filenames and I wish to find the checksum of the each file and store in a list displaying [filename,checksum].

New to programming so I tried creating a for loop taking the files from directory. I then used hashlib.md5 to open file with its path and print the filename alongside the checksum.

directory = os.listdir(path)
    def file_as_bytes(file):
                with file:
                    return file.read()
    for fx in directory:
        pass
        print[(fx, hashlib.md5(file_as_bytes(open(fx, 'rb'))).digest())]

This is the error I obtain:

IOError: [Errno 2] No such file or directory: 'c.txt'

Which I never created in my client. I only wish to display the checksum of each file that I have in my client (that already exist)

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
B2A3R9C9A
  • 29
  • 6
  • `open(...).read()` is shorter than `file_as_bytes(open(...))` =) – lenik Oct 24 '19 at 17:06
  • and you definitely want to print `.hexdigest()`, not `.digest()` of md5, because the latter is pretty much binary and contains unprintable characters. – lenik Oct 24 '19 at 17:07

1 Answers1

2

Instead of :

open(fx, 'rb')

use:

open(os.path.join( path, fx), 'rb')
lenik
  • 23,228
  • 4
  • 34
  • 43