【问题标题】:How to pass in parameter in a sys.argv[] code -Python如何在 sys.argv[] 代码中传递参数-Python
【发布时间】:2018-06-06 00:55:37
【问题描述】:

我试图传递我的dir,但我似乎不理解系统行参数..这是一个示例代码,用于检查目录中的重复文件,来自here...我不知道'不知道如何插入我自己的路径作为参数

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

【问题讨论】:

标签: python-3.x


【解决方案1】:

我做了脚本方法... 但我也通过删除 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多