基本的病毒扫描递归

时间:2012-11-05 02:53:53

标签: python recursion virus

因此我编写的代码采用pathnamesignature,第三个参数是number,表示应扫描的子目录的深度。

所以我想说我有一个文件: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()

1 个答案:

答案 0 :(得分:2)

for循环并不像那样。他们的语法是for <variable> in <iterable>

请改为使用if语句:

if depth <= 0:
    return

for item in os.listdir(pathname):
相关问题