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:
- if age<21:
- print "You cannot buy wine!\n"
- print "But you can buy chewing gum.\n"
- 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.
- #!/usr/bin/env Python
- #coding=utf-8
- import os
- import zipfile
- filelist = []
- for root, dirs, files in os.walk("D:\\clean"):
- for name in files:
- filelist.append(os.path.join(root, name))
- zf = zipfile.ZipFile("d:\\test.zip", "w", zipfile.zlib.DEFLATED)
- for tar in filelist:
- zf.write(tar)
- zf.close()
The above is an introduction to Python compressed files.