如何在sys.argv []代码-Python中传入参数

时间:2018-06-06 00:55:38

标签: python-3.x

我试图传递我的dir,但我似乎不理解系统行参数。有一个示例代码检查dir中的重复文件,来自here。 ..i不知道如何插入我自己的Path作为参数

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

请问如何通过我的path ..?

1 个答案:

答案 0 :(得分:0)

我做了脚本方法...... 但我还是通过删除 if sys.argv[1:]: check_for_duplicates(sys.argv[1:]) else: print("Please pass the paths to check as parameters to the script")

来找到更好的方法

并将其替换为main函数以执行代码 if __name__ == '__main__': check_for_duplicates(yourPaths)