
Python 2 代码:
import os a = '谢谢大家帮助我。' b = a.decode('utf-8').encode('gbk') command = '7z x test.zip -p%s' % (b,) os.system(command) 问题描述:
test.zip压缩包,密码为“谢谢大家帮助我。”这个压缩包是在 Windows 环境下用 winrar5.5 或者 winzip22 创建的。请问在 Python 3 里面应该怎么写才能让 os.system 正确执行?
补充说明:
import os a = '谢谢大家帮助我。' b = bytes(a,'gbk').decode('utf-8',errors='replace') command = '7z x t.zip -p%s' % (b,) os.system(command) 1 kokutou Apr 1, 2018 via Android 不如看看 python 库:zipfile ? |
2 gnaggnoyil Apr 1, 2018 8102 年了怎么还有分不清 code point 和 byte 的人.`a.encode("gbk")`不就完了 |
3 Gandum OP @kokutou 这个我考虑过,不过 zipfile 库好像只能在 open 和 extract 时候输密码,我只是想 test 我的压缩包们(事实上我想执行的是 7z t ) @gnaggnoyil 我之前不知道 os.system 可以输入 bytes,经你启发发现这一点,问题解决,Thank you ! |
4 gnaggnoyil Apr 2, 2018 @Gandum 讲道理你要是知道`errors='replace'`是在干什么的话就不会有这种问题了…… |
5 Gandum OP @gnaggnoyil 恩,我会去仔细看看的!不过这位朋友还请留步一下,请问在 zipfile 里面怎么解决这个问题呢? 这里 ZipFile.extractall(pwd='谢谢大家帮助我。'.encode('gbk'))就不起作用了,提示“ Bad password ”/div> |
6 Gandum OP @gnaggnoyil 我又研究了一下,zipfile 这个模块似乎本身就有 bug,它无法识别用 Winrar 创建的任何加密 zip,即使密码是纯数字,比如 1234 之类 |
7 gnaggnoyil Apr 2, 2018 @Gandum 没用过 zipfile 这个库,抱歉我也没法解答…… |
8 whoami9894 Apr 2, 2018 via Android @Gandum python3 下的 zipfile 我用过,没问题,编码用 Unicode |