【发布时间】: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..?
【问题讨论】:
-
如果您创建一个Minimal, Complete, and Verifiable 示例,您将获得更多更好的答案。
-
代码是正确的...我只是不明白
sys.argv[1:]部分...我如何传递我的 DirPath ? -
请注意Minimal, Complete, and Verifiable中的Minimal。请删除与问题无关的任何内容。
-
调用脚本
python myscript.py path1 path2时将路径作为参数传递
标签: python-3.x