Home > Blockchain >  Scan computer system file using class definition
Scan computer system file using class definition

Time:10-08

I am new to python and rarely use class definition, I try to run my program but had error occur... Anyone know what happen? Or I had code something wrong? I had been trying for 2days... This is coding below:

import hashlib
import os.path
import os

class QuickScan:
    def md5(self,fname):
        hash_md5 = hashlib.md5()
        try:
            with open(fname, "rb") as f:
                for chunk in iter(lambda: f.read(2 ** 20), b""):
                    hash_md5.update(chunk)
        except Exception:
            pass
        return hash_md5.hexdigest()

    def get_all_abs_paths(self,rootdir):
        viruslist = open('C:/FYP/SecuCOM2022/virusshare.md5.txt','rt')
        virusinside = [l.rstrip() for l in viruslist]
        paths = list()
        virus="detected"
        novirus="clear"
    
        for dirpath,_,filenames in os.walk(rootdir):
            for f in filenames:
              
             paths.append(os.path.abspath(os.path.join(dirpath, f)))

        for filename in paths:
            print(filename, self.md5(filename))
            if self.md5(filename) in virusinside:
                print(virus)
                os.remove(filename)
            else:
                print(novirus)
        

    filenames=get_all_abs_paths('C:/Users/User/Desktop/irustesting')

Below is the error occur:-

class QuickScan:
  File "c:\FYP\SecuCOM2022\QuickScanTab.py", line 71, in QuickScan

filenames=get_all_abs_paths('C:/Users/User/Desktop/irustesting')
TypeError: get_all_abs_paths() missing 1 required positional argument: 'rootdir'  

CodePudding user response:

get_all_abs_paths() is instance ttribute (method) of class QuickScan. You need to instantiate object of class QuickScan and call its get_all_abs_paths()

scan = QuickScan()
scan.get_all_abs_paths('C:/Users/User/Desktop/irustesting')

Also, note that this method does not have explicit return, so it does return None and your code where you bind the return value to name filenames does not make much sense.

Also, you need to unindent it one level, at the moment filenames=get_all_abs_paths('C:/Users/User/Desktop/irustesting') is inside the class.

  • Related