-1

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 "ioerror errno 13 permission denied: 'C:\pagefile.sys'" when I try to run the file from C:\ is there a way i can run this as an admin? Even when I run cmd as an admin that does not work, thank you in advance.

import os, hashlib

current_dir = os.getcwd()
for root,dirs,files in os.walk(current_dir):
    for f in files:
        current_file = os.path.join(root,f)
        H = hashlib.md5()

        with open(current_file) as FIN:
            H.update(FIN.read())
            with open("gethashes.txt", "a") as myfile:
                myfile.write(current_file),myfile.write(",      "),myfile.write(H.hexdigest()),myfile.write("\n")

        print current_file, H.hexdigest()
dperrie
  • 315
  • 1
  • 2
  • 11
  • if not file is `locked` : http://stackoverflow.com/questions/19672352/how-to-run-python-script-with-elevated-privilege-on-windows – dsgdfg Jul 21 '16 at 10:24
  • 1
    *pagefile.sys* should be skipped. Even if you were able to read it, the contents may change any time and the generated md5 hash would be incorrect. – J.J. Hakala Jul 21 '16 at 12:53
  • Adding to what @J.J.Hakala said, it may even change when you are reading it. – Braiam Jul 21 '16 at 18:53

1 Answers1

0

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.

Dinesh Pundkar
  • 4,160
  • 1
  • 23
  • 37
  • When running this code I get the following error: ValueError: I/O operation on closed file. However, if I include this line "with open("gethashes.txt", "a") as myfile:" I get further and through the process but still receive an error: IOError: [Errno 13] Permission denied: 'C:\\Boot\\BCD' – dperrie Jul 21 '16 at 12:48
  • @dperrie - Updated the code. Please check and let me know – Dinesh Pundkar Jul 21 '16 at 18:52