Skip to content

Instantly share code, notes, and snippets.

@yuhangch
Last active December 3, 2020 10:49
Show Gist options
  • Save yuhangch/15c015df98a4fe47e0435534cf6285ad to your computer and use it in GitHub Desktop.
Save yuhangch/15c015df98a4fe47e0435534cf6285ad to your computer and use it in GitHub Desktop.
Python Zipfile 中文文件名乱码
from zipfile import ZipFile
from pathlib import Path
target_directory = "."
with ZipFile(src, 'r') as f:
for file_name in f.namelist():
file_name = f.extract(file_name, path=target_directory)
try:
fn2 = file_name.encode('cp437').decode('gbk')
except:
fn2 = file_name.encode('utf-8').decode('utf-8')
extracted_path = Path(file_name)
extracted_path.rename(fn2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment