如何使用7zip压缩而不是zip,代码更改

zer*_*cer 9 python

我有一个代码用zip压缩特定文件夹中的每个文件,但我想用7zip压缩它,那该怎么办?

这是我到目前为止:

for date in dict_date:#zipping folders and get same name like the folder
    with ZipFile(os.path.join(src, '{0}.7z'.format(date)), 'w') as myzip:
        for subFolder in dict_date[date]:
            for fil in os.listdir(os.path.join(src, date, subFolder)):
                if not fil.endswith('.7z'):
                    myzip.write(os.path.join(src, date, subFolder, fil))
Run Code Online (Sandbox Code Playgroud)

Rak*_*esh 9

您可以尝试命令行方法

import subprocess
subprocess.call(['7z', 'a', filename+'.7z', filename])
Run Code Online (Sandbox Code Playgroud)

或者对于文件夹中的所有文件

subprocess.call(['7z', 'a', filename+'.7z', "*.*"])
Run Code Online (Sandbox Code Playgroud)

  • 谁使用`.7zip`扩展名?我见过的每个7-zip文件都是`.7z`. (3认同)