本帖最后由 sakura32 于 2023-11-15 16:37 编辑
如题,我需要查看压缩包内部文件结构,但是使用7zip的l指令后读取的stdout无法显示特殊字符,被替换为了"_",不清楚是7zip的问题还是cmd的问题。
代码如下:
[Python] 纯文本查看 复制代码 import subprocess
path_7zip = '7-Zip/7z.exe'
filepath = r'F:\解压测试❤.zip'[img]https://i.postimg.cc/pd6bzptW/QQ-20231115091828.png[/img]
command_l = [path_7zip, "l", filepath]
process_l = subprocess.run(command_l,
stdout=subprocess.PIPE,
creationflags=subprocess.CREATE_NO_WINDOW,
text=True,
universal_newlines=True)
if process_l.returncode == 0:
stdout_line = process_l.stdout
print(stdout_line)
原文件名为"解压测试❤.zip",但是stdout中为"解压测试_.zip"(❤是一个实心爱心字符)
stdout输出结果:
[Plain Text] 纯文本查看 复制代码 7-Zip 23.01 (x64) : Copyright (c) 1999-2023 Igor Pavlov : 2023-06-20
Scanning the drive for archives:
1 file, 360559 bytes (353 KiB)
Listing archive: F:\解压测试_.zip
--
Path = F:\解压测试_.zip
Type = zip
Physical Size = 360559
Date Time Attr Size Compressed Name
------------------- ----- ------------ ------------ ------------------------
2023-10-02 13:09:00 ....A 1173284 360169 文件1.xlsx
2023-11-02 13:44:02 ....A 21 24 文件2.txt
------------------- ----- ------------ ------------ ------------------------
已经试过的方法:
1.subprocess中添加参数encoding='utf-8',会报错UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbd in position 162: invalid start byte
2.stdout+encode+decode,无法解决 |