添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
import zipfile

注意:安装完unrar后,from unrar import rarfile可能会报错: Couldn’t find path to unrar library.

windows和Linux的解决方法可以参考博客:
https://blog.csdn.net/ysy950803/article/details/52939708

def un_tgz(filename):      # filename是文件的绝对路径
       tar=tarfile.open(filename)
       #判断是否存在同名文件夹,若不存在则创建同名文件夹:
       if os.path.isdir(os.path.splitext(filename)[0]):
       else:
           os.mkdir(os.path.splitext(filename)[0])
       tar.extractall(os.path.splitext(filename)[0])
       tar.close()
def un_rar(filename):    # filename是文件的绝对路径
    rar=rarfile.RarFile(filename)
    #判断是否存在同名文件夹,若不存在则创建同名文件夹:
    if os.path.isdir(os.path.splitext(filename)[0]):
    else:
        os.mkdir(os.path.splitext(filename)[0])
    rar.extractall(os.path.splitext(filename)[0])
def un_zip(filename):   # filename是文件的绝对路径
       zip_file=zipfile.ZipFile(filename)
       #判断同名文件夹是否存在,若不存在则创建同名文件夹
       if os.path.isdir(os.path.splitext(filename)[0]):
       else:
           os.mkdir(os.path.splitext(filename)[0])
       for names in zip_file.namelist():
           zip_file.extract(names,os.path.splitext(filename)[0])
       zip_file.close()

上面三个代码运行后,会产生一个和压缩文件同名的的文件夹,解压后的文件就在这个同名文件夹里面:。再读取就好了

先安装相关的包:import osimport tarfileimport unrar # 直接pip install unrarfrom unrar import rarfile import zipfile注意:安装完unrar后,from unrar import rarfile可能会报错:Couldn’t find path to unrar l... gz: 即gzip。通常仅仅能压缩一个文件。与tar结合起来就能够实现先打,再压缩。 tar: linux系统下的打工具。仅仅打。不压缩 tgz:即tar.gz。先用tar打,然后再用gz压缩得到的文件 zip: 不同于gzip。尽管使用相似的算法,能够打压缩多个文件。只是分别压缩文件。压缩率低于tar。 rar:打压缩文件。最初用于DOS,基于window操作系统。 压缩率比zip高,但速度慢。随机訪问的速度也慢。 关于ziprar之间的各种比較。可见: http:/
一. 读取 rar 文件 Pythonpython-unrar 模块依赖于 UnRAR library, 所有先安装 UnRAR library 1. 安装 UnRAR library 在官网可以找到各种版本的安装, 下面给出 Linux 版安装的说明 $ cd ~ $ wget http://www.rarlab.com/rar/unrarsrc-5.2.6.tar.gz $ tar -...