The 11th Class Python standard library overview

Source: Internet
Author: User

10.python Standard Library Overview

10.1 Operating System interface

The OS module provides many functions that interact with the operating system (Operating system)

Name of function

How to use

-GETCWD ()

Return to current working directory

-chdir (PATH)

Change the working directory

-listdir (path= ".")

Enumerates the file names in the specified directory (".") Represents the current directory ,"..." Indicates the previous level of the directory )

-mkdir (PATH)

Create a single-level directory, such that a thrown exception already exists in the directory

-makedirs (PATH)

Recursive creation of multi-level catalogs , such as a thrown exception that already exists in the directory , note :"e:\\a\\b" and "e:\\a\c" do not conflict

-remove ()

deleting files

-rmdir (PATH)

Delete a single-level directory , which throws an exception if the directory is not empty

-removedirs (PATH)

Recursively deletes directories , from subdirectories to parent directories, and attempts to remove them , encountering directory non-null throw exceptions

-rename (old,new)

Rename the file old to new

-system (command)

Shell command to run the system

You should use the import OS style instead of the from OS import *. This ensures that os.open (), which varies with the operating system, does not overwrite the built-in function open ()

using some large modules like the OS is built -in dir () and help () functions are useful:

Import osdir (OS) <returns a list of all module functions>help (OS) <returns an extensive mnaual page created from th E module ' s docstring>

for daily file and directory management tasks,the Shutil module provides an easy-to-use, high-level interface:

10.2 File wildcard characters

The Glob module provides a function for generating a list of files from a directory wildcard search:

10.3 command-line arguments

generic tool scripts often invoke named row parameters. These command-line parameters are stored in a linked list in the argv variable of the SYS module . For example, after executing python demo.py One two three on the command line , you can get the following output:

Import Sysprint (SYS.ARGV) [' demo.py ', ' one ', ' I ', ' three ']

10.4 Error output redirection and program termination

SYS also has the stdin,stdout and stderr Properties, even when the stdout is redirected, The latter can also be used to display warnings and error messages:

the direct termination of most scripts uses sys.exit ()

10.5 string Regular match

The RE module provides a regular expression tool for advanced string processing. For complex matching and processing, regular expressions provide a concise, optimized solution:

The string method is best used for simple operation, easy to read:

10.6 Mathematics

The Math module provides access to the underlying C function library for floating-point operations :

Random provides a tool for generating the stochastic number:

The ScipyProject provides a number of numerical calculation modules

10.7 Internet Access

There are several modules for accessing the Internet and for processing network communication protocols. The simplest of these two are the urllib.request for processing data received from URLs and the smtplib for sending e-mails :

This module is very important in the following web crawler.

10.8 Date and Time

The datetime module provides both simple and complex methods for date and time processing. While supporting date and time algorithms, the implementation focuses on more efficient processing and formatting of the output. The module also supports time zone processing.

10.9 Data Compression

the following modules directly support the common data packaging and compression formats:zlib,gzip,bz2,lzma,zipfile and tarfile.

10.10 Performance Metrics

some 1 users are interested in understanding the performance differences between different approaches to solving the same problem. Python provides a measurement tool. For example, using the meta-ancestor encapsulation and unpacking to exchange elements looks much more tempting than using traditional methods. Timeit proves that the latter is faster:

The profile and pstats modules provide a time measurement tool for larger blocks of code, relative to the Timeit's fine-grained size .

10.11 Quality Control

One way to develop high-quality software is to develop test code for each function and test it frequently during the development process.

The Doctest module provides a tool for scanning modules and performing tests based on the document strings embedded in the program. The test construct is as simple as cutting and pasting its output results into a document string. Through the user-provided example, it developed the documentation that allows the Doctest module to confirm that the result of the code is consistent with the document:

The UnitTest module is not as easy to use as the Doctest module, but it can provide a more comprehensive set of tests in a separate file:

10.12 Swiss Army Knife

    • The xmlrpc.client and Xmlrpc.server modules make remote procedure calls a breeze. Although the module has such a name, the user does not need to have knowledge of XML or process XML.
    • An email package is a library that manages mail information, including MIME and other RFC2822-based information documents.
    • Unlike the Smtplib and poplib modules that actually send and receive information , the email package contains a complete toolset to construct or parse complex message structures, including attachments, and to implement Internet encoding and header protocols.
    • The Xml.dom and Xml.sax packages provide powerful support for the popular Information Interchange format. Similarly, the CSV module supports direct read and write in the common database format.
    • together, these modules and packages greatly simplify the Data exchange between Python applications and other tools
    • internationalization is supported by GetText, locale and codecs packages.

  

  

The 11th Class Python standard library overview

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.