ioerror errno 13权限被拒绝:' C:\\ pagefile.sys'

时间:2016-07-21 10:17:52

标签: python os.walk

下面是我的代码,我想要实现的是通过操作系统生成代码运行的每个文件的MD5哈希,但是,我收到标题中的错误" ioerror errno 13权限被拒绝: ' C:\ PAGEFILE.SYS'"当我尝试从C:\运行该文件时,有没有办法可以作为管理员运行它?即使我作为管理员运行cmd不起作用,也要提前感谢。

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 个答案:

答案 0 :(得分:0)

如错误中所述 - 权限被拒绝 - 因为需要读取文件才能获取其内容的md5。如果我们没有读取权限,总会有这样的情况。

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)

请告诉我它是否有用。