有趣生活

当前位置:首页>科技>python批量解压提取pdfpython编程批量解压RAR文件

python批量解压提取pdfpython编程批量解压RAR文件

发布时间:2026-07-21阅读(1)

导读一、前言本文实现RAR批量解压的功能,通过python脚本调用WinRAR.exe解压文件时似乎不会再有广告框弹出。二、实现通过python调用WinRAR....一、前言

本文实现RAR批量解压的功能,通过python脚本调用WinRAR.exe解压文件时似乎不会再有广告框弹出。

二、实现

通过python调用WinRAR.exe程序实现RAR文件的批量解压,代码如下:

import argparseimport osclass RarExtractor: def __init__(self, in_dir="./", out_dir="./", pwds=None, exe=None): self.in_dir = in_dir self.out_dir = out_dir self.pwds = pwds if pwds else [1234] self.exe = "%s" % exe if exe else "C:\Program Files\WinRAR\WinRAR.exe" def extract_files(self, pwds, file_path, dst): if not os.path.exists(dst): os.mkdir(dst) if os.path.isdir(dst) and os.path.isfile(file_path): try: for pwd in pwds: extract_cmd = r%s x -y -p%s %s %s % (self.exe, pwd, file_path, dst) if os.system(extract_cmd) == 0: print("Extract %s OK." % file_path) return 0 else: print("Extract %s failed." % file_path) return -1 except RuntimeError: print("error") return -1 else: print(File not exist) return -1 def extract_all_rar(self): for root, dirs, files in os.walk(self.in_dir): for f in files: (filename, ext) = os.path.splitext(f) if ext == .rar: file_path = os.path.join(root, f) print(file_path) self.extract_files(self.pwds, file_path, os.path.join(self.out_dir, filename))def _parse_options(): parser = argparse.ArgumentParser() parser.add_argument("--in_dir", action="store", dest="in_dir", required=True, help="Rar files dir") parser.add_argument("--out_dir", action="store", dest="out_dir", required=False, help="Extracted file dir") parser.add_argument("--pwds", nargs= , action="store", dest="pwds", required=False, help="Password list to extract Rar: --pwds 1111 2222 3333") parser.add_argument("--exe", action="store", dest="exe", required=False, help="RAR exe install path") return parser.parse_args()if __name__ == __main__: options = _parse_options() extractor = RarExtractor(options.in_dir, options.out_dir, options.pwds, options.exe) extractor.extract_all_rar()

需要传入的参数为:

  • --in_dir RAR文件所在目录,默认是当前目录
  • --out_dir 指定解压后输出的目录,默认是当前目录
  • --pwds 如果RAR是加密的,则需要指定解压密码,可以指定多个密码,以空格隔开
  • --exe 指定WinRAR.exe所在的目录,默认是"C:\Program Files\WinRAR\WinRAR.exe"

三、测试

在目录D:\rar_test\下新建3个txt文件,使用RAR加密压缩,密码为1024、2048和4096。

通过以下命令测试:

python rar_extractor.py --in_dir D:\rar_test\ --out_dir D:\rar_test\ --pwds 1024 2048 4096 --exe "C:\Program Files\WinRAR\WinRAR.exe"

TAGS标签:  python  批量  解压  提取  pdfpython  python批量解压

Copyright © 2024 有趣生活 All Rights Reserve吉ICP备19000289号-5 TXT地图HTML地图XML地图