1

i have tried to pass in my dir but i don't seem to understand system line argument..here is a sample code that checks for duplicate files in a dir, gotten from here...i don't know how to insert my own Path as parameter

def chunk_reader(fobj, chunk_size=1024):
    """Generator that reads a file in chunks of bytes"""
    while True:
        chunk = fobj.read(chunk_size)
        if not chunk:
            return
        yield chunk

def check_for_duplicates(paths, hash=hashlib.sha1):
    hashes = {}
    for path in paths:
        for dirpath, dirnames, filenames in os.walk(path):
            for filename in filenames:
                full_path = os.path.join(dirpath, filename)
                hashobj = hash()
                for chunk in chunk_reader(open(full_path, 'rb')):
                    hashobj.update(chunk)
                file_id = (hashobj.digest(), os.path.getsize(full_path))
                duplicate = hashes.get(file_id, None)
                if duplicate:
                    print("Duplicate found: %s and %s" % (full_path, duplicate))
                else:
                    hashes[file_id] = full_path

if sys.argv[1:]:
    check_for_duplicates(sys.argv[1:])
else:
    print("Please pass the paths to check as parameters to the script")

pls how do i pass in my path..?

X-Black...
  • 1,376
  • 2
  • 20
  • 28

1 Answers1

0

i did the script method... but i also got around with a better way by removing if sys.argv[1:]: check_for_duplicates(sys.argv[1:]) else: print("Please pass the paths to check as parameters to the script")

and replace it with a main function to execute the code if __name__ == '__main__': check_for_duplicates(yourPaths)

X-Black...
  • 1,376
  • 2
  • 20
  • 28