Scene:
ZIP file operation, including additions and deletions
1.zip creation, and create a file inside the ZIP package
>>> Import zipfile>>> handler=open (' Temp_zip.zip ', ' W ') >>> handler.close () >>> z= ZipFile. ZipFile (' C:/python34/temp_zip.zip ', ' W ') >>> z.writestr (' hello.py ', ' Def f (): Return "Hello Word from" +__file_ _\n ') >>> z.close ()
2. Pack the file tree into the zip
We preset a folder and a number of files, and then pack a folder tree into the zip inside, and finally we output the file name in the package
Output the names of all files recursively by NameList
3.. empty the Zip package
>>> Z=zipfile. ZipFile (' Temp_zip.zip ', ' W ') >>> Z.close ()
4. Unzip
>>> Import zipfile>>> z=zipfile. ZipFile (' Temp_zip.zip ', ' a ') >>> Z.extractall () >>> z.close ()
The above method is extracted to the root directory of Python, if you want to extract to another directory, you can add a parameter address after the Extractall
Right here, thank you.
------------------------------------------------------------------
Click to jump 0 basic python-Catalogue
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Easy Python file feature-some operations on zip files