Python tutorial (10): browse standard library (1)

Source: Internet
Author: User
10.1 Operating System Interface

The OS module provides many functions for interaction with the operating system:

Are you sure you want to use the import OS style instead of from OS import *. This will prevent OS. open () from being overwritten by the built-in open () function, and its operations are completely different.

The built-in functions Dir () and help () are used as interaction assistants for large modules like OS:

For daily file and directory management tasks, the shutil module provides a more advanced interface, which is easy to use:

10.2 file wildcard

The glob module provides a function to retrieve the file list from the directory wildcard search:

10.3 command line parameters

Common functional scripts often need to process command line parameters. These parameters are stored in the argv attribute of the SYS module and are a list. For example, the following is the output result of running Python demo. py One Two Three from the command line:

The getopt module processes SYS. argv and uses the Unix getopt () function convention. The argparse module provides enhanced and flexible command line justification.

10.4 error output redirection and program termination

The SYS module also has attributes stdin, stdout, and stderr. The next one is very useful for issuing warning and error messages and making them visible, even when stdout has been redirected:

The most direct way to terminate a script is to use SYS. Exit ().

10.5 String Matching

The re module provides regular expressions for advanced string processing. Regular Expressions provide concise and optimized solutions for complex matching and operations:

When you only need simple functions, the string method is preferred because they are easy to read and Debug:

10.6 mathematics

The math module accesses the underlying C language library for floating point mathematics:

The random module provides tools for random number selection:

The scipy project has many other modules for numerical operations.

10.7 Internet access

Many modules are used to access the Internet and process Internet protocols. Urllib. request is used to obtain data from the URL and smtplib is used to send mail:

10.8 Date and Time

The datetime module provides classes for performing operations on dates and times in a simple and complex way. Date and Time arithmetic is supported, and the focus of implementation is on extracting valid numbers for output formatting and operations. The module also supports Time Zone-aware objects:

10.9 Data Compression

Common Data Archiving and compression formats are directly supported by the module: zlib, Gzip, bz2, lzma, zipfile, and tarfile:

10.10 performance measurement

Some Python users are interested in the relative performance of different solutions to the same problem. Python provides a measurement tool to answer these questions immediately.

For example, we try to use the tuples packaging and package settlement feature instead of the traditional method to exchange parameters. The timeit module quickly demonstrates a modest performance benefit:

Compared with timeit, the profile and pstats modules provide tools to mark key time parts in large pieces of code.

10.11 Quality Control

One way to develop high-quality software is to write and test each function during development and often run these tests during development.

The doctest module provides a tool for scanning a module and Verifying Tests embedded in a program's document string. The test structure is simple: cut and paste a typical Call and its results into the document string. This improves the document by providing examples to users, and it allows the doctest module to confirm that the code is still true for the document:

The unittest module is not as easy as the doctest module, but it allows a more comprehensive test set to be maintained in a separate file:

10.12 included frameworks

Python has a "Framework inclusion" philosophy. With the experience and strong ability of most of its packages, we can better see this point. For example:

  • The XMLRPC. Client and XMLRPC. Server modules enable Remote Procedure Calls to be implemented in an almost unimportant task. Despite the module name, there is no direct knowledge or necessary XML for processing.
  • An email package is a library used to manage email messages, including mime and other rfc2822-based message documents. Unlike smtplib and poplib, they actually send and receive messages. The e-mail package has a complete set of tools for building or decoding complex message structures (including attachments) and implementing internet encoding and header protocols.
  • The XML. Dom and XML. Sax packages provide strong support for parsing popular Data Interaction formats. Similarly, the CSV module supports direct reading and writing in the format of a common database. In short, these modules and packages greatly simplify the Data Interaction Between Python applications and other tools.
  • Some modules are supported for internationalization, including the gettext, locale, and codecs packages.

This article is the official website content translation, original address:

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: 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.