I'm trying to write a program somewhat similar to old virus scanners, where I walk from the root directory of a system and find the md5 checksum for each file, and then compare it to a checksum inputted by the user. I'm running into an issue where while the script walks through the file system and reads then converts the data into the md5 checksum, certain files will basically stall the program indefinitely with no error message.
I have a try/except to check whether the file is readable before I try reading the file and getting the md5, and every time the program stalls, it'll be on the same files, and it will get stuck on the f.read()
function. My code is below and I appreciate any help. I'm using the try/except the way that the os.access()
documentation suggests.
def md5Check():
flist = []
md5list = []
badlist = []
for path, dirname, fname in os.walk(rootdir):
for name in fname:
filetype = os.access(os.path.join(path, name), os.R_OK)
item = os.path.join(path, name)
try:
ft = open(item, 'rb')
ft.close()
if filetype== True:
with open(item, 'rb') as f:
md5obj = hashlib.md5()
fdata = f.read()
md5obj.update(fdata)
md5list.append(md5obj.hexdigest())
print(f'try: {item}')
except (PermissionError, FileNotFoundError, OSError, IOError):
badlist.append(item)
print(f'except:{item}')
Also, keep in mind that the functionality for comparing a user-entered checksum is not yet coded in, as i cant even get a full array of md5 checksums to compare to, since it stalls before walking the whole filesystem and converting them to md5
I've also tried using the os.access() method with os.R_OK, but the documentation says that its an insecure way to check for readability, so i opted for the simple try/except with open() instead
Lastly, this program should run the same on windows/linux/macos (and everything so far does, including this persistent issue), so any OS specific solutions wont work for this particular project
Any help is appreciated!