rar文件处理

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:\Python35\lib\site-packages\rarfile.py", line 2813, in custom_popen
    creationflags=creationflags)
  File "C:\Python35\lib\subprocess.py", line 676, in __init__
    restore_signals, start_new_session)
  File "C:\Python35\lib\subprocess.py", line 955, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。

下载UnRar.exe ,放在当前文件夹,运行x.py

posted @ 2020-01-14 17:19  一只小小的寄居蟹  阅读(190)  评论(0编辑  收藏  举报