1.下载UnRAR.exe,与python文件放在同一目录下
2.使用rarfile模块
代码:
def rar_f(self, fz_name, path): """ 解压缩文件 :param fz_name: zip文件 :param path: 解压缩路径 :return: """ flag = False try: if rarfile.is_rarfile(fz_name): # 检查是否为rar文件 rf = rarfile.RarFile(fz_name) # 待解压文件 rf.extractall(path) # print( # "%s is running [%s] " % # (currentThread().getName(), fz_name)) print('解压缩文件[{}]成功'.format(fz_name)) flag = True except Exception as e: print('解压缩文件[{}]失败'.format(fz_name), str(e)) return {'file_name': fz_name, 'flag': flag} rar_f('F:\test\2019.rar','F:\test')
错误处理
Traceback (most recent call last): File "C:Python35libsite-packages arfile.py", line 2813, in custom_popen creationflags=creationflags) File "C:Python35libsubprocess.py", line 676, in __init__ restore_signals, start_new_session) File "C:Python35libsubprocess.py", line 955, in _execute_child startupinfo) FileNotFoundError: [WinError 2] 系统找不到指定的文件。
下载UnRar.exe ,放在当前文件夹,运行x.py