2012-11-05 101 views
0

所以我正在編寫的代碼需要pathnamesignature,第三個參數是number,它代表了應該掃描的子目錄的深度。基本病毒掃描遞歸

所以可以說我有一個文件:測試

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

所有我需要的是在正確的方向一推,或一些建議。但我是一個新手,所以對我很容易。 –

+1

你有什麼問題? –

+0

我在執行深度時遇到問題 –

回答

2

for循環並不像那樣。他們的語法是for <variable> in <iterable>

只是我們的if語句來代替:

if depth <= 0: 
    return 

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

感謝您的幫助。 –