tarfile 和zipfile解压zip、tar文件

1.zip 解压缩

import os
import zipfile

target_file = r",/file.zip"
extract_path = "./data"

def unzip_file(zip_file_path, extract_to_path):
    with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
        zip_ref.extractall(extract_to_path)

    print('Extract zip file finished!')

unzip_file(target_file, extract_path)

2. tar 解压缩

import tarfile

def untar_file(tar_file_path, extract_to_path):
    with tarfile.open(tar_file_path, 'r') as tar_ref:
        tar_ref.extractall(extract_to_path)

3.tar.gz 解压缩

import tarfile

def untar_gz_file(tar_gz_file_path, extract_to_path):
    with tarfile.open(tar_gz_file_path, 'r:gz') as tar_gz_ref:
        tar_gz_ref.extractall(extract_to_path)
正文完
 
admin
版权声明:本站原创文章,由 admin 2023-11-26发表,共计584字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请联系tensortimes@gmail.com。
评论(没有评论)
验证码