首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基本病毒扫描递归

基本病毒扫描递归
EN

Stack Overflow用户
提问于 2012-11-05 02:53:53
回答 1查看 2.2K关注 0票数 0

因此,我正在编写接受pathnamesignature和第三个参数的代码,第三个参数是number,它表示应该扫描的子目录的深度。

因此,假设我有一个文件: test

代码语言:javascript
复制
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

,所以它应该是这样工作的:

代码语言:javascript
复制
>>>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

这里是我的当前代码:

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-05 03:03:14

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

相反,我们只需要一个if语句:

代码语言:javascript
复制
if depth <= 0:
    return

for item in os.listdir(pathname):
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13225358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档