0

pathnameそのため、 a 、 aを受け取るコードを書いていますsignature。3 番目のパラメーターは、numberスキャンする必要があるサブディレクトリの深さを表す a です。

だから私はファイルを持っているとしましょう: 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()
4

1 に答える 1

2

forループは実際にはそのようには機能しません。それらの構文はfor <variable> in <iterable>.

代わりに私たちにif声明を出してください:

if depth <= 0:
    return

for item in os.listdir(pathname):
于 2012-11-05T03:03:14.203 に答える