python 读写.tar.gz文件 -- UnicodeDecodeError

  在用pip install 安装库的时候,偶尔会出现编码错误(如:UnicodeDecodeError: 'gbk' codec can't decode byte),对此我们可先将包下载下来(一般为.tar.gz格式),然后修改其中的错误代码,再执行本地安装即可。

import tarfile
import os

#下载库安装包
pip download wlab

#库包解压缩与压缩
def untar(fname, dirs):
    t = tarfile.open(fname)
    t.extractall(path = dirs) 
    
def tar(fname):
    t = tarfile.open(fname + ".tar.gz", "w:gz")
    for root, dir, files in os.walk(fname):
        print(root, dir, files)
        for file in files:
            fullpath = os.path.join(root, file)
            t.add(fullpath)
    t.close()

untar('wlab-1.1.5.tar.gz', '.')  #'.'表示解压到当前目录,'./wlab',对解压后文件修正
tar('wlab-1.1.5')

#安装库
pip install c:\users\epsoft\wlab-1.1.5.tar.gz

  

参考资料:

python tar.gz格式压缩、解压

 

posted on 2019-05-24 00:56  iUpoint  阅读(2775)  评论(0编辑  收藏  举报

导航