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格式壓縮、解壓

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM