Python IOError: Errno 13 权限被拒绝 [英] Python IOError: Errno 13 Permission denied

查看:109
本文介绍了Python IOError: Errno 13 权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我完全不知所措.我整晚都在研究这个,但我无法让它发挥作用.我有权查看文件,我想做的就是阅读该死的东西.每次我尝试我都会得到:

Ok, I'm totally baffled. I've been working on this all night and I can't get it to work. I have premission to look into the file, all I want to do is read the darn thing. Every time I try I get:

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    scan('test', rules, 0)
  File "C:\Python32\PythonStuff\csc242hw7\csc242hw7.py", line 45, in scan
    files = open(n, 'r')
IOError: [Errno 13] Permission denied: 'test\\test'

这是我的代码.它尚未完成,但我觉得我至少应该为我正在测试的部分获得正确的值.基本上我想查看一个文件夹,如果有文件扫描,它会查找我将 signatures 设置为的任何内容.如果有文件夹,我将根据指定的 depth 扫描或不扫描它们.如果有 depth <0 然后它会返回.如果 depth == 0 那么它只会扫描第一个文件夹中的元素.如果 depth >0 它将扫描文件夹直到指定的深度.这些都不重要,因为无论出于何种原因我都没有读取文件的权限.我不知道我做错了什么.

Here is my code. It's unfinished but I feel I should at least be getting correct values for the sections I am testing. Basically I want to look into a folder, and if there is a file scan it looking for whatever I set my signatures to. If there are folders I will or will not scan them depending on the depth specified. If there is a depth < 0 then it will return. If depth == 0 then it will just scan the elements in the first folder. If depth > 0 it will scan folders up until the specified depth. None of that matters thought, because for whatever reason I don't have permission to read the file. I have no idea what I am doing wrong.

def scan(pathname, signatures, depth):
'''Recusively scans all the files contained in the folder pathname up
until the specificed depth'''
    # Reconstruct this!
    if depth < 0:
        return
    elif depth == 0:
        for item in os.listdir(pathname):
            n = os.path.join(pathname, item)
            try:
                # List a directory on n
                scan(n, signatures, depth)
            except:
                # Do what you should for a file
                files = open(n, 'r')
                text = file.read()
                for virus in signatures:
                    if text.find(signatures[virus]) > 0:
                        print('{}, found virus {}'.format(n, virus))
                files.close()

只需快速

下面的代码做了一些非常相似的事情,但我无法控制深度.但是,它工作正常.

This code below does something very similar, but I can't control the depth. It however, works fine.

def oldscan(pathname, signatures):
    '''recursively scans all files contained, directly or
       indirectly, in the folder pathname'''
    for item in os.listdir(pathname):
        n = os.path.join(pathname, item)
        try:
            oldscan(n, signatures)
        except:
            f = open(n, 'r')
            s = f.read()
            for virus in signatures:
                if s.find(signatures[virus]) > 0:
                    print('{}, found virus {}'.format(n,virus))
            f.close()

推荐答案

我大胆猜测 test\test 是一个目录,发生了一些异常.您盲目地捕获异常并尝试将目录作为文件打开.这在 Windows 上给出了 Errno 13.

I venture to guess that test\test is a directory, and some exception occurred. You catch the exception blindly and try to open the directory as a file. That gives Errno 13 on Windows.

使用os.path.isdir来区分文件和目录,而不是try...except.

Use os.path.isdir to distinguish between files and directories, instead of the try...except.

    for item in os.listdir(pathname):
        n = os.path.join(pathname, item)
        if os.path.isdir(n):
            # List a directory on n
            scan(n, signatures, depth)
        else:
            # Do what you should for a file

这篇关于Python IOError: Errno 13 权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆