如何使用python解压缩和解压缩?

问题描述

因此,我需要访问压缩文件夹(也为tar)中的文件文件夹。用户提供存在此Gunzip文件的路径,并从python代码提取路径,我需要将所有这些文件解压缩,解压缩并将其解压缩到同一位置,然后访问此目录中的文件文件夹。

path given by user - C:/Users/user1/Desktop/tar_gz/tarball.tar.xz

文件提取到同一目录-C:/ Users / user1 / Desktop / tar_gz / tarball

我对机器学习的概念是陌生的,很难解决这个问题。有什么方法可以做到这一点?

解决方法

Python tarfile module

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