import rarfileimport randomimport timeimport sysclass MyIterator():# 单位字符集合letters = '0123456789'min_digits = 0max_digits = 0def __init__(self, min_digits, max_digits):# 实例化对象时给出密码位数范围,一般4到10位if min_digits < max_digits:self.min_digits = min_digitsself.max_digits = max_digitselse:self.min_digits = max_digitsself.max_digits = min_digits# 迭代器访问定义def __iter__(self):return selfdef __next__(self):rst = str()for item in range(0, random.randrange(self.min_digits, self.max_digits + 1)):rst += random.choice(MyIterator.letters)return rstdef extract():start_time = time.time()zfile = rarfile.RarFile("D:\\deng\\logs\\datastruture.rar")# 解压文件名字for p in MyIterator(4, 10):try:zfile.extractall(path=".", pwd=str(p).encode('utf-8'))# 文件地址这里用.说明当前文件夹,如果C:\Users这样形式print("the password is {}".format(p))now_time = time.time()print("spend time is {}".format(now_time - start_time))sys.exit(0)except Exception as e:print('error',p)passif __name__ == '__main__':extract()
1200字范文,内容丰富有趣,写作的好帮手!