2012-11-05 2 views
0

pathname, signature, 그리고 세 번째 매개 변수는 스캔해야하는 하위 디렉터리의 깊이를 나타내는 number입니다.기본 바이러스 스캔 재귀

그래서 나는 파일을 가지고 있다고 할 수 있습니다 : 시험 :

: 여기

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

내 현재 코드입니다 :

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 

그래서이 작동하는 방법이다

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

. 그러나 나는 초심자이므로 나에게 쉽게 가라. –

+1

당신이 가지고있는 문제는 무엇입니까? –

+0

깊이 구현에 문제가 있습니다. –

답변

2

for 루프는 실제로 그렇게 작동하지 않습니다. 구문은 for <variable> in <iterable>입니다. 대신

그냥 우리 if 문 : 내가 필요로하는 모든이 올바른 방향으로 푸시, 또는 어떤 조언이다

if depth <= 0: 
    return 

for item in os.listdir(pathname): 
+0

도움을 주셔서 감사합니다. –