ioerror errno 13 permission denied: 'C:\\pagefile.sys'

空扰寡人 提交于 2020-01-07 08:37:41

问题


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()

回答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.



来源:https://stackoverflow.com/questions/38501476/ioerror-errno-13-permission-denied-c-pagefile-sys

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!