This article mainly introduces the ZipFile module in Python to use the detailed, ZipFile module is used to manipulate the zip file, the need for friends can refer to the
The zip file format is a common document compression standard, in the Ziplib module, the ZipFile class is used to manipulate the zip file, the follow
This article mainly introduces how to use the zipfile module in Python. The zipfile module is used to operate zip files. If you need it, refer to the zip file format below, which is a common document compression standard, in the ziplib module, the ZipFile class is used to operate zip files. The following describes the
The Python programming language is an object-oriented programming language with powerful functions. It is easy to use and grasp. Today, let's take a look at one of the most important application modules and the related application methods of the Python ZipFile module.
Interpretation of Python compressed file basic ap
The Python zipfile module is used to compress and decompress zip code. zipfile contains two very important classes: ZipFile and ZipInfo. In most cases, we only need to use these two classes. ZipFile is the main class used to create and read zip files, while ZipInfo is the in
about how Python handles Word docs doc docx, you can focus on the Python-docx and python-docx2txt two projects, python-docx more complex and appropriate to create documents, Python-docx2txt makes it easy to convert documents to txt:
https://
in dictionary format data = con["Section_a" ["Keya1"] Print (data) # output Valuea1? # The Judgment node is not in the object print ("Section_a" in con) # output True other additions and deletions to check grammar Import Configparser ? Con = configparser. Configparser () #实例化创建一个对象 con.read ("Test.ini") #调用对象的read方法读取配置文件 ? Con.add_section ("New_section") #添加一个节点, the node name is New_section, at which point the added node has not been written to the file,
Python has the most abundant and powerful class libraries in the scripting language and is sufficient to support most daily applications. Python modules and C can work together and can be embedded into C or C ++ applications, therefore, the Python language can be used to provide script interfaces for applications.
Here we will record how to use
The Python zipfile module is used to compress and decompress the ZIP format code, ZipFile has two very important classes, ZipFile and Zipinfo, in the vast majority of cases, we just need to use the two class. ZipFile is the primary class that is used to create and read zip f
1) Simple application of ZipFileIf you are simply using Python for compression and decompression, the method is as followsImportZipfilef= ZipFile. ZipFile ('Filename.zip','W', ZipFile. zip_deflated) F.write ('1.txt') F.write ('2.doc') F.write ('3.zip') F.close () F.zipfile.zipfile ('Filename') F.extractall () f.close (
Python comes with a zipfile module for reading and writing zip files.def zip_dir (dirname,zipfilename): filelist = [] if Os.path.isfile (dirname): filelist.append (dirname) else: for root, dirs, files in Os.walk (dirname): For name in Files: filelist.append (Os.path.join (Root , name)) ZF = ZipFile.
Python zipfile Module
#! /Usr/bin/env pythonimport zipfileimport OS # create a compress file for/etcz = zipfile. zipFile ('/root/etc.zip', 'w') for root, dirs, files in OS. walk ('/etc'): for file in files: z. write (OS. path. join (root, file) z. close () # check the name list of the compressed file 'etc.zip 'z =
#!/usr/bin/env python#-*-coding:utf-8-*- fromZipFileImport*ImportZipFile#Unzip the zip filedefunzip (): Source_zip="C:\\update\\sw_servers_20120815.zip"Target_dir="c:\\update\\"Myzip=ZipFile (source_zip) myfilelist=myzip.namelist () forNameinchMyfilelist:f_handle=open (Target_dir+name,"WB") F_handle.write (myzip.read (name)) F_handle.close () myzip.close ()#add a file to an existing ZIP packagedefAddzip ()
#-*-CODING=GBK-*-import zipfiledef UnZip (Path, patht): #path for the file path that needs to be decompressed, patht for the extracted target directory f = zipfile.ZipFile (path, ' R ') print "Start extracting files ..." For file in F.namelist (): print "Extracting file:%s to%s"% (file, patht) F.extract (file, patht)Under Windows Python uses the above code to extract the zip file, found that the decompression will be the last modification time of the
This article mainly introduces the example of using the memory zipfile object to package files in the memory in python. For more information, see the following code:
Import zipfileImport StringIO
Class InMemoryZip (object ):Def _ init _ (self ):# Create the in-memory file-like objectSelf. in_memory_zip = StringIO. StringIO ()
Def append (self, filename_in_zip, file_contents ):'''Appends a file with name fi
Recently read the ZIP compression package in Python. To report a mistake.Python 2.6.6 (r266:84292, June, 14:18:47) [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on Linux2type "help", "copyright", "Credits" or "license" for more information.>>> import zipfile>>> ZipFile. ZipFile (' bla.apk ') Traceback (most recent call last):
ZipFile. ZipFile (FILE,MODE,COMPRESS_TYPE,ALLOWZIP64) #创建一个ZipFile对象File:zip file nameMode: ' R ' reads the existing zip file, ' W ' new or overwrites the existing zip file, ' a ' appended to the existing ZIP documentCompress_type:zipfile. Zip_stored is not compressed by default, ZipFile. zip_deflated compressionALLOWZ
Copy Code code as follows:
Import ZipFile
Import Stringio
Class Inmemoryzip (object):def __init__ (self):# Create the In-memory File-like objectSelf.in_memory_zip = Stringio.stringio ()
def append (self, filename_in_zip, file_contents):' Appends a file with name Filename_in_zip and contents ofFile_contents to the in-memory zip. '# Get a handle to the ' in-memory zip in append modeZF = ZipFile.
The code is as follows:
Import ZipFileImport Stringio
Class Inmemoryzip (object):def __init__ (self):# Create the In-memory File-like objectSelf.in_memory_zip = Stringio.stringio ()
def append (self, filename_in_zip, file_contents):"Appends a file with name Filename_in_zip and contents ofFile_contents to the in-memory zip. "# Get A handle to the In-memory zip in append modeZF = ZipFile. ZipFile (Self.in_me
The main problem is to solve the compression and decompression of empty folders in compressed directory.Functions for compressing folders:1# coding:utf-82 Import OS3 Import ZipFile4 5 def zipdir (dirtozip,savepath):6 ifNot Os.path.isdir (dirtozip):7Raise Exception,u"zipdir error,not a dir '% '". Format (dirtozip)8 9(savedir,_) =os.path.split (Savepath)Ten ifNot Os.path.isdir (savedir): One os.makedirs (Savedir) A -Ziplist = [] - the forRoot,dirs,filesinchOs.walk (dirtozip): -
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.