简体   繁体   English

Python IOError:Errno 13权限被拒绝

[英]Python IOError: Errno 13 Permission denied

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'

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. 基本上,我想查看一个文件夹,如果有文件扫描,它将查找我设置signatures的内容。 If there are folders I will or will not scan them depending on the depth specified. 如果有文件夹,我会或不会扫描它们,具体取决于指定的depth If there is a depth < 0 then it will return. 如果depth < 0 ,它将返回。 If depth == 0 then it will just scan the elements in the first folder. 如果depth == 0那么它将仅扫描第一个文件夹中的元素。 If depth > 0 it will scan folders up until the specified depth. 如果depth > 0 ,它将扫描文件夹直到指定深度。 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()

Just a quick edit: 只需快速编辑:

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

I venture to guess that test\\test is a directory, and some exception occurred. 我敢猜测test\\test是一个目录,并且发生了一些异常。 You catch the exception blindly and try to open the directory as a file. 您盲目地捕获了异常,然后尝试将目录作为文件打开。 That gives Errno 13 on Windows. 这样就可以在Windows上使用Errno 13。

Use os.path.isdir to distinguish between files and directories, instead of the try...except. 使用os.path.isdir区分文件和目录,而不是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM