基本病毒扫描递归

2024-04-29 20:19:44 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我编写的代码接受一个pathname,一个signature,第三个参数是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

所以它应该这样工作:

^{pr2}$

这是我当前的代码:

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

Tags: and代码inpyforscanisfolder
1条回答
网友
1楼 · 发布于 2024-04-29 20:19:44

for循环并不是这样工作的。它们的语法是for <variable> in <iterable>。在

只需使用if语句:

if depth <= 0:
    return

for item in os.listdir(pathname):

相关问题 更多 >