日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

python linux解密zip_Python Linux系统管理之使用Python管理压缩包

發布時間:2024/1/23 linux 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python linux解密zip_Python Linux系统管理之使用Python管理压缩包 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、使用tarfile庫讀取與創建tar包

0、先創建一些測試文件

touch {1..2}.txt

1、創建壓縮包

In [7]: import tarfile

In [9]: with tarfile.open('demo.tar',mode='w') as out:

...:out.add('1.txt')

...:out.add('2.txt')

...:---------------------------------------------------------------------------FileNotFoundError Traceback (most recent call last) in

1 with tarfile.open('demo.tar',mode='w') as out:----> 2 out.add('1.txt')3 out.add('2.txt')4# 最后一空行直接回車即可

2、讀取tar包

In [3]: with tarfile.open(‘demo.tar’) as t:

…: for file in t.getmembers():

…: print(file.name)

…:

1.txt

2.txt

3、創建壓縮包

with tarfile.open(‘demo.tar.gz’,mode=’w:gz’) as out:

…: out.add(‘1.txt’)

…: out.add(‘2.txt’)

…:

4、讀取壓縮包

In [6]: with tarfile.open(‘demo.tar.gz’,mode=’r:gz’) as out:

…: for f in out.getmembers():

…: print(f.name)

…:

1.txt

2.txt

5、提取單個或者所有文件

In [14]: with tarfile.open(‘demo.tar.gz’,mode=’r:gz’) as out:

…: out.extract(‘1.txt’)

…:

In [15]: ls

1.txt anaconda-ks.cfg demo.tar demo.tar.gz

In [16]: with tarfile.open(‘demo.tar.gz’,mode=’r:gz’) as out:

…: out.extractall()

…:

In [17]: ls

1.txt 2.txt anaconda-ks.cfg demo.tar demo.tar.gz

二、使用zipfile庫創建和讀取壓縮包

1、創建zip文件

In [1]: import zipfile

In [2]: newZip = zipfile.ZipFile(‘demo.zip’,’w’)

In [3]: newZip.write(‘1.txt’)

In [4]: newZip.write(‘2.txt’)

In [5]: newZip.close()

In [6]: ls

1.txt 2.txt anaconda-ks.cfg demo.tar demo.tar.gz demo.zip

2、讀取zip文件

In [7]: newZip = zipfile.ZipFile(‘demo.zip’)

In [8]: newZip.namelist()

Out[8]: [‘1.txt’, ‘2.txt’]

3、解壓zip文件

In [12]: newZip.extract(‘1.txt’)

Out[12]: ‘/root/1.txt’

In [13]: newZip.extractall()

In [14]: ls

1.txt 2.txt anaconda-ks.cfg demo.tar demo.tar.gz demo.zip

三、使用shutil管理壓縮包

In [1]: import shutil

In [2]: shutil.make_archive(‘demo’,’zip’)

Out[2]: ‘demo.zip’

In [3]: shutil.make_archive(‘demo’,’gztar’)

Out[3]: ‘demo.tar.gz’

In [4]: shutil.unpack_archive(‘demo.tar.gz’)

In [5]: shutil.unpack_archive(‘demo.zip’)

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的python linux解密zip_Python Linux系统管理之使用Python管理压缩包的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。