解压fi时出现Unicode错误

2024-09-29 01:32:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有我的压缩文件,其中有一些文件要解压缩。我的函数运行良好,直到我的一些文件在其他名称中使用unicode,如下所示:

- myfile.wav
- myfile2.wav
- my§ile.wav

当我尝试用这个函数解压时

with closing(z), zipfile.ZipFile(io.BytesIO(z.content)) as myzip:
                myzip.extractall(local_path)

我得到一个unicode错误

如何更正文件名


Tags: 文件函数名称mywithunicodemyfileclosing
1条回答
网友
1楼 · 发布于 2024-09-29 01:32:03

在传递到extractall方法之前,应该对文件进行解码

with closing(z), zipfile.ZipFile(io.BytesIO(z.content)) as myzip:
            local_path = local_path.decode('utf-8')
            myzip.extractall(local_path)

读取模块中的documentation,它们在以下方面是明确的:

There is no official file name encoding for ZIP files. If you have unicode file names, you must convert them to byte strings in your desired encoding before passing them to '(). WinZip interprets all file names as encoded in CP437, also known as DOS Latin.

相关问题 更多 >