Python 基本病毒扫描递归

Python 基本病毒扫描递归,python,recursion,virus,Python,Recursion,Virus,因此,我正在编写代码,其中包含一个路径名、一个签名,第三个参数是一个数字,它表示应该扫描的子目录的深度 假设我有一个文件:test: In it is test1 folder,test2 folder,antivirus.py,simple.py In test1 folder is antivirus1.py In test2 folder is test3 folder, and antivirus2.py In test3 folder is antivirus3.py 这就是它的工作

因此,我正在编写代码,其中包含一个
路径名
、一个
签名
,第三个参数是一个
数字
,它表示应该扫描的子目录的深度

假设我有一个文件:test

In it is test1 folder,test2 folder,antivirus.py,simple.py
In test1 folder is antivirus1.py
In test2 folder is test3 folder, and antivirus2.py
In test3 folder is antivirus3.py
这就是它的工作原理:

>>>scan('test',rules, 0)
test\antivirus.py, found virus Virus2
test\antivirus.py, found virus Virus1
>>>
>>>scan('test',rules, 2)
test\antivirus.py, found virus Virus2
test\antivirus.py, found virus Virus1
test\test1\antivirus1.py, found virus Virus2
test\test1\antivirus1.py, found virus Virus1
test\test2\antivirus2.py, found virus Virus2
test\test2\antivirus2.py, found virus Virus1
def scan(pathname, signatures, depth):
    for item in os.listdir(pathname) and depth > 0:
        n = os.path.join(pathname, item)
        try:
            scan(n, signatures, depth-1)
        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()
这是我当前的代码:

>>>scan('test',rules, 0)
test\antivirus.py, found virus Virus2
test\antivirus.py, found virus Virus1
>>>
>>>scan('test',rules, 2)
test\antivirus.py, found virus Virus2
test\antivirus.py, found virus Virus1
test\test1\antivirus1.py, found virus Virus2
test\test1\antivirus1.py, found virus Virus1
test\test2\antivirus2.py, found virus Virus2
test\test2\antivirus2.py, found virus Virus1
def scan(pathname, signatures, depth):
    for item in os.listdir(pathname) and depth > 0:
        n = os.path.join(pathname, item)
        try:
            scan(n, signatures, depth-1)
        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()

for
循环实际上不是这样工作的。它们的语法是
for in

请给我们一个
if
语句:

if depth <= 0:
    return

for item in os.listdir(pathname):

如果有深度,我只需要往正确的方向推一下,或者给点建议。但我是个新手,所以对我放松点。你有什么问题吗?我在实现深度方面有困难