python - 如何使用python解压缩和解压?
问题描述
所以我需要访问压缩文件夹(也是tar)中的文件和文件夹。用户提供此 Gunzip 文件所在的路径,从 python 代码中,我需要将所有这些文件解压缩、解压缩并将其提取到同一位置,然后访问此目录中的文件和文件夹。
path given by user - C:/Users/user1/Desktop/tar_gz/tarball.tar.xz
文件应解压缩到同一目录 - C:/Users/user1/Desktop/tar_gz/tarball
我是机器学习概念的新手,很难弄清楚这一点。有什么方法可以做到这一点吗?
解决方案
import tarfile
import os
path_tofile = r"C:/Users/user1/Desktop/tar_gz/tarball.tar.xz"
extract_direcotry = os.path.dirname(path_tofile)
if tarfile.is_tarfile(path_tofile):
with tarfile.open(path_tofile) as f:
f.extractall(path=extract_direcotry) # Extract all members from the archive to the current working directory
推荐阅读
- javascript - 使用canvg从svg获取png失败
- javascript - nodemailer 发件人为空
- python - 如何根据使用 pandas 的另一列 [SoldDate] 找到特定列 [Model] 的计数?
- python - 如何在 .txt 文件中提取特定范围的行?
- graphql - 按照 GraphQL 教程/解析器对象查询 MySQL 数据库失败
- python - Bash 命令可以在 shell 中运行,但不能通过 python suprocess.run
- angular - 如何使用 Typescript 为 Angular 反应表单声明模型类
- javascript - 如何在不刷新页面的情况下重定向到锚标记?
- javascript - 当我单击阅读更多时,如何使每个框展开
- spring-boot - 如何修复'试图从空的一对一属性分配 id'