0

Is there any way to determine the md5 hash values of only directories in a filesystem?

I don't want the hash value of files. I only want the values for the directories and subdirectories.

jwpfox
  • 5,124
  • 11
  • 45
  • 42

2 Answers2

0

I think it does not question to python but to OS:

>>> print(hashlib.md5(open('/home/tuls/dev', 'rb').read()).hexdigest())
...
IsADirectoryError: [Errno 21] Is a directory: '/home/tuls/dev'

and in console the same:

~$ md5sum /home/tuls/dev/
md5sum: /home/tuls/dev/: Is a directory

It's because directory doesn't contain data, just files which have data. Look:

~$ du -hs Documents/
2,9G    Documents/

~$ ls -lhd Documents/
drwxr-xr-x 5 tuls tuls 4,0K гру  2 11:21 Documents/

There is 2,9G in directory, but weight of directory is 4,0K (just metadata)

The answer to your question is - md5 hash not applicable for directories, only for files.

tulsluper
  • 1,688
  • 1
  • 13
  • 8
0

If you really need to calculate a hash of the directory - you may just calculate hash of the path of this directory. (As was already mentioned, directory may not be hashable object of File System in specific OS)

For example:

my_dir_path = 'C:\some\dir\name'
my_dir_hash = hash(my_dir_path)

This solution is cross-platform, fast and robust.

KromviellBlack
  • 908
  • 6
  • 10