python標準庫之zipfile__python

來源:互聯網
上載者:User
python標準庫zipfile

什麼是zip檔案?為何在網路上zip打包的檔案比較常見。而不是rar?

zip是一種壓縮歸檔的檔案,zip開源的。

python的zipfile模組,有兩個主要的類
ZipFile()

函數,判斷是否為zip類型檔案

zipfile. is_zipfile ( filename )

Returns True if filename is a valid ZIP file based on its magic number,otherwise returns False. filename may be a file or file-like object too
直接從壓縮檔中讀取檔案內容
ZipFile. read ( name [, pwd ] ) # 實際上調用了self.open().read()

Return the bytes of the file name in the archive. name is the name of thefile in the archive
ZipFile. open ( name [, mode [, pwd ] ] )

Extract a member from the archive as a file-like object (ZipExtFile)
ZipFile. close ( )

Close the archive file. You must call close() before exiting your programor essential records will not be written. ZipInfo()

# 壓縮到指定檔案def file_zip(src, dst):    zip_obj = ZipFile(dst, mode='w')    zip_obj.write(src)    zip_obj.close() # 解壓縮def unzip(src):    zip_obj = ZipFile(src, mode='r')    for info in zip_obj.filelist:        zip_obj.extract(member=info)    zip_obj.close()


zipfile的命令列介面

Usage:    zipfile.py -l zipfile.zip        # Show listing of a zipfile    zipfile.py -t zipfile.zip        # Test if a zipfile is valid    zipfile.py -e zipfile.zip target # Extract zipfile into target dir    zipfile.py -c zipfile.zip src ... # Create zipfile from sources

-c Create

-e Extract(提取)

-l List


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.