Skip to content Skip to sidebar Skip to footer

Ioerror Errno 13 Permission Denied: 'C:\\pagefile.sys'

Below is my code, what I am trying to achieve is walking through the OS generating a MD5 hash of every file the code is functional, however, I receive the error in the title 'ioerr

Solution 1:

As mentioned in error - Permission denied - since file need to be read for getting md5 of its content. There will be always a case when we don't have read permission .

import os, hashlib

def md5_chk(current_file):
    try:
        md5 = ''
        err = ''
        H = hashlib.md5()
        with open(current_file) as FIN:
            H.update(FIN.read())
            md5 = H.hexdigest()
    except Exception, e:
        md5 = None
        err = str(e)
        print err
    return md5,err

if __name__ == '__main__':    
    current_dir = os.getcwd()
    for root,dirs,files in os.walk(current_dir):
        with open("G://gethashes.txt", "a") as myfile:
            for f in files:
                current_file = os.path.join(root,f)
                md5_val,err = md5_chk(current_file)
                if md5_val is not None:
                     myfile.write(current_file),myfile.write(",    "),myfile.write(md5_val),myfile.write("\n")
                     print current_file, md5_val
                else:
                     myfile.write(current_file),myfile.write(",    "),myfile.write("Error - " + str(err)),myfile.write("\n")
                     print current_file, str(err)

Please let me know if it is useful.


Post a Comment for "Ioerror Errno 13 Permission Denied: 'C:\\pagefile.sys'"