日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

『Python学习笔记』ZipFile解压中文路径乱码[以及rar、7z解压方法]

發布時間:2023/12/20 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 『Python学习笔记』ZipFile解压中文路径乱码[以及rar、7z解压方法] 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
ZipFile解壓中文路徑亂碼[以及rar、7z解壓方法]

文章目錄

    • 一. zip、7z、rar解壓方法
    • 二. 參考文章

一. zip、7z、rar解壓方法

import patoolib import zipfile import py7zrdef decode_path(path):'''zipfile解壓出現亂碼,將亂碼的路徑編碼為UTF8'''try:path_name = path.decode('utf-8')except:path_name = path.encode('437').decode('gbk')path_name = path_name.encode('utf-8').decode('utf-8')return path_namedef extract_zip_rar_7z(file, target_dir):""" 解壓rar、zip、7z文件,:param file: 壓縮文件路徑:param target_dir: 解壓到的目錄:return:"""try:if file.endswith(".zip"): # .zip格式文件解壓## 解壓方式1:存在亂碼# f = zipfile.ZipFile(file, mode='r')# f.extractall(target_dir)## 解壓方式2:防止亂碼with zipfile.ZipFile(file, allowZip64=True) as Z:# 排除目錄文件print("Z.filelist", Z.filelist)file_iter = (fileone for fileone in Z.filelist if os.path.isfile(file))for file_one in file_iter:# 編碼文件名稱為 utf 格式file_one.filename = decode_path(file_one.filename) # 防止亂碼的操作Z.extract(file_one, target_dir)elif file.endswith(".rar"): # .rar格式文件解壓patoolib.extract_archive(file, outdir=target_dir)else: # .7z格式文件解壓f = py7zr.SevenZipFile(file, mode='r')f.extractall(target_dir)except Exception as e:print(f"文件解壓失敗:{e}")

二. 參考文章

  • ZipFile 解壓中文路徑亂碼解決方案:https://zhuanlan.zhihu.com/p/431495395
  • python zip文件解壓中文亂碼解決方案:https://zhuanlan.zhihu.com/p/450817759

總結

以上是生活随笔為你收集整理的『Python学习笔记』ZipFile解压中文路径乱码[以及rar、7z解压方法]的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。