新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python如何生成tar文件內(nèi)容
tarfile包中的.open(name, mode)方法能夠以mode指定的方式打開name壓縮文件,并返回一個TarFile類對象。調(diào)用TarFile對象的extractall(path)方法可以將tar文檔解壓到path指定的位置。

10年積累的成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有山海關(guān)免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
import tarfile tar = tarfile.open( '*.tar.gz', mode = "r:gz") #"r:gz"表示 open for reading with gzip compression tar.extractall(path='temp') ### 將tar.gz文件解壓到temp文件夾下 tar.close()
open返回的對象不但可以用來讀文檔數(shù)據(jù)('r': reading),還可以寫('w': writing),附加('a': appending)。
相關(guān)推薦:《python教程》
如下是mode取值所對應(yīng)的含義:
'r' or 'r:*' open for reading with transparent compression 'r:' open for reading exclusively uncompressed 'r:gz' open for reading with gzip compression 'r:bz2' open for reading with bzip2 compression 'r:xz' open for reading with lzma compression 'a' or 'a:' open for appending, creating the file if necessary 'w' or 'w:' open for writing without compression 'w:gz' open for writing with gzip compression 'w:bz2' open for writing with bzip2 compression 'w:xz' open for writing with lzma compression 'x' or 'x:' create a tarfile exclusively without compression, raise an exception if the file is already created 'x:gz' create a gzip compressed tarfile, raise an exception if the file is already created 'x:bz2' create a bzip2 compressed tarfile, raise an exception if the file is already created 'x:xz' create an lzma compressed tarfile, raise an exception if the file is already created 'r|*' open a stream of tar blocks with transparent compression 'r|' open an uncompressed stream of tar blocks for reading 'r|gz' open a gzip compressed stream of tar blocks 'r|bz2' open a bzip2 compressed stream of tar blocks 'r|xz' open an lzma compressed stream of tar blocks 'w|' open an uncompressed stream for writing 'w|gz' open a gzip compressed stream for writing 'w|bz2' open a bzip2 compressed stream for writing 'w|xz' open an lzma compressed stream for writing
名稱欄目:創(chuàng)新互聯(lián)Python教程:python如何生成tar文件內(nèi)容
網(wǎng)站地址:http://www.dlmjj.cn/article/cocseph.html


咨詢
建站咨詢
