python3解压命令
时间: 2023-10-26 10:27:25 浏览: 79
解压命令详解
在Python3中,可以使用不同的库来进行解压命令,具体取决于要解压的文件类型。
如果要解压7z文件,可以使用py7zr库。以下是一个示例代码:
```python
import py7zr
# 解压缩7z文件
with py7zr.SevenZipFile("Archive.7z", 'r') as archive:
archive.extractall(path="/tmp")
# 压缩文件为7z
with py7zr.SevenZipFile("Archive.7z", 'w') as archive:
archive.writeall("target/")
```
如果要解压zip文件,可以使用zipfile库。以下是一个示例代码:
```python
import os
import zipfile
# 压缩文件
def make_zip(source_path, output_filename):
zip_file = zipfile.ZipFile(output_filename, 'w')
pre_len = len(os.path.dirname(source_path))
for parent, dirnames, filenames in os.walk(source_path):
for filename in filenames:
print(f'{filename}')
path_file = os.path.join(parent, filename)
arcname = path_file<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [Python3,9行代码进行压缩和解压缩,从此告别压缩软件。](https://blog.csdn.net/wuyoudeyuer/article/details/123956825)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文