Python decompression and compression code for ZIP and tar.gz

Source: Internet
Author: User
Tags create zip

Import gzip# Compression g = gzip. Gzipfile (filename= "", mode= ' WB ', Compresslevel=9, Fileobj=open (R ' r:\test.log.gz ', ' WB ')) G.write (open (R ' R:\test.log '). Read ()) g.close () #解压g = gzip. Gzipfile (mode= ' RB ', Fileobj=open (R ' r:\popopo.gz ', ' RB ')) Open (R ' R:\test.log ', ' WB '). Write (G.read ())
Import Osimport tarfile# compression, create tar.gz package # Create ZIP package name tar = Tarfile.open ("/tmp/tartest.tar.gz", "W:gz") #创建压缩包for Root,dir,files In Os.walk ("/tmp/tartest"): For file in Files:fullpath = Os.path.join (root,file) tar.add (FullPath) tar.close () # Unzip the tar.gz package method one: Unzip to the specified directory tar = Tarfile.open (Tar_path, "r:gz") File_names = Tar.getnames () for file_name in File_names:tar.ext Ract (File_name,target_path) Tar.close () method Two: tar = Tarfile.open ("tartest.tar.gz") tar.extract ("/tmp") Tar.close ()

Python decompression and compression code for ZIP and tar.gz

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.