Interpretation of Python compressed file basic application code example

Source: Internet
Author: User
Tags zipinfo

The emergence of the Python programming language has greatly improved the programming efficiency, and its application method is simple and easy to use, this greatly satisfies the development needs of some programmers. We will introduce in detail how to compress Python files today.

  • Python dictionary addition and deletion operations
  • A variety of common Python dictionary Application Methods
  • Python PDB simple debugging method
  • Summary of common Python Image Processing Techniques
  • Interpretation of the basic application of Python Regular Expressions

Before learning about the Python compressed file technique, let's give a detailed introduction to the Python language.

Scalability is a feature of Python as a programming language. New built-in module) can be written in C or C ++. We can also add Python interfaces to existing modules. Python allows you to focus on the program tasks to be implemented by avoiding excessive syntax constraints.

Python is also known as a clear language. The general guiding ideology of its authors when designing it is that there is only one best solution to a specific problem. This is expressed in The python motto "The Zen of Python" written by Tim Peters:

There shoshould be one -- and preferably only one -- obvious way to do it.

Interestingly, this is exactly the opposite of the central idea of TMTOWTDIThere's More Than One Way To Do It. This seems to be an important reason why people often compare Perl with Python.

The Python language is a clear language. Another meaning is that its authors intentionally design highly restrictive syntaxes to make bad programming habits, such as not indent to the right of the next line of the if statement) cannot be compiled. This intentionally forces programmers to develop good programming habits. One of the most important items is the indentation rule of Python.

For example, if statement:

 
 
  1. if age<21: 
  2. print "You cannot buy wine!\n"  
  3. print "But you can buy chewing gum.\n"  
  4. print "this is outside if\n" 

The difference between a module and most other languages, such as C) is the boundary of a module, it is determined by the position of the first character of each line in this line, while the C language uses a pair of curly braces {} to clearly define the boundary of the module, is irrelevant to the character position ). This has caused controversy. Since the birth of a language like C, the Syntactic meaning of the language is separated from the character arrangement, and it has been regarded as a progress of a programming language. But it is undeniable that Python makes the program clearer and more beautiful by forcing programmers to indent all the places where they need to use modules, such as if, for, and function definition.

In addition, Python also adheres to a clear and uniform style in the design of other parts, which makes Python a language that is easy to use, easy to maintain, and popular and widely used by a large number of users. The program segments directly written in Python sometimes run more efficiently than programs written in C.

Zipfile is the compression and decompression module used for zip encoding in Python. zipfile has two very important classes: ZipFile and ZipInfo. ZipFile is the main class used to create and read zip files, while ZipInfo is the information of each file stored in the zip file.

Here I need to compress a directory, which requires adding files in the directory, files, and files, and then when using the zipfile class of ZipFile, write the compressed file one by one to complete the Python compressed file operation step.

 
 
  1. #!/usr/bin/env Python  
  2. #coding=utf-8  
  3. import os  
  4. import zipfile  
  5. filelist = []  
  6. for root, dirs, files in os.walk("D:\\clean"):  
  7. for name in files:  
  8. filelist.append(os.path.join(root, name))  
  9. zf = zipfile.ZipFile("d:\\test.zip", "w", zipfile.zlib.DEFLATED)  
  10. for tar in filelist:  
  11. zf.write(tar)  
  12. zf.close() 

The above is an introduction to Python compressed files.

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.