Python Learning (6) module-standard module and python learning standard module
Python standard module
Python comes with a standard module library and has released independent documents (Library Reference Manual ). For programmers, the standard library is equally important as the language itself. It is like a treasure chest that provides a perfect solution for a variety of common tasks.
This section briefly introduces the commonly used Python standard libraries, including operating system interfaces, file wildcards, system modules, string Regular Expression matching, and mathematics (previously described in the int and float sections), Internet access, date, and time.
Using these large modules, dir () and help () functions are more useful. Of course, the standard library is far more than that. Learn more ~
Standard module storage location: In the lib folder under the Python directory, for example, C: \ Python34 \ Lib. If necessary, you can view the corresponding module source files.
OS-Operating System Interface
List only several common methods
OS. name: returns the name of the current operating system.
OS. environ all environment variables of the current system
OS. getenv (key) gets the value of an environment variable. If no value exists, None is returned.
OS. getpid () returns the current process PID (process-related methods are more useful in UNIX systems. For more information, see the corresponding standard library Documentation)
OS. getlogin () returns the logon username of the current system.
OS. system (...) Run shell command
OS. getcwd () to get the current working directory
OS. chdir (path) change the current working directory
OS. listdir (path) lists all files and folder names in the directory.
Function methods related to OS. path... folder and file operations are not listed at the moment. {operations on this topic learning file will be listed in the future}
1 import OS 2 print (dir (OS) 3 # help (OS) 4 print (OS. name) # current operating system name, which defines 'posix'-Linux/Unix, 'nt '-Windows, 'mac', 'os2', 'ce ', 'java' 5 print (OS. environ) # All environment variables of the current system 6 print (OS. getenv ('homepath') # returns the value of an environment variable. If no value exists, None 7 8 print (OS. getpid () # returns the current process PID 9 print (OS. getlogin () # Return the username of the current system logon 10 11 OS. system ('cmd') # OS. system () to run shell commands is equivalent to starting dos12 13 print (OS. getcwd () # Get the current working directory 14 # OS. chdir ('d: \ test ') # change the current working directory to d: \ test15 16 print (OS. listdir ('d: \ ') # list all files and folder names in the directory 17 # help (OS. path)
Glob -- file wildcard
The glob module provides a function to generate a file list from the directory wildcard search.
Refer to the following example, glob. glob ("*. py ") returns all the current working directories (changed to" C: \ Python34 \ Lib "), with the wildcard ". you can also specify an absolute path, such as print (glob. glob ("C :\\ Python34 \ Lib \\*. py "))
The following table lists the wildcard characters that can be used in the wildcard mode. These wildcards are not necessarily the same as those in the shell command of the operating system, but the glob module of Python uses the same syntax on all platforms.
Note that the wildcard mode syntax is similar to but different from the regular expression syntax.
Wildcard matching example
* 0 or multiple arbitrary characters *. m * match the name with the extension starting with m
? Any single character ??? Match a name that exactly contains three characters
[…] Any character [AEIOU] * listed in square brackets matches the name starting with an uppercase vowel.
[!...] Any character that does not appear in square brackets *[! S] match names that do not end with s
Sys-system module
sysThe module contains the functions of the system.
Command Line Parameters: Common tool scripts often call command line parameters, which are stored in the argv variable of the sys module as a linked list.
For example, create a test. py file in the current directory with the following content:
Run the command in cmd, such as python test. py 111 222.
The getopt module uses the Unix getopt () function to process sys. argv. More reasons for complex command line processing: The argparse module provides
Python version
Sys. version provides the version information of the installed Python. sys. version_info is a tuples that indicate the Python version information.
1 import sys2 print(sys.version)3 print(sys.version_info)
Operating system type
Sys. platform provides system platform types, such as win32 linux2.
1 import sys2 print(sys.platform)
Error output redirection and program termination
Sys has the stdin, stdout, and stderr attributes. Even when stdout is redirected, the latter can also be used to display warnings and error messages.
Most scripts use sys. exit () for targeted termination ()
Re -- string regular match
The re module provides a regular expression tool for advanced string processing. Here is a brief introduction. {learn Regular Expressions for this topic in the future }.
The regular expression may not be as efficient as the built-in str method, but the function is very powerful. Thanks to this, in the language that provides regular expressions, the syntax of regular expressions is the same, the difference is that the number of syntax is different; the Unsupported syntax is usually not commonly used.
If you have already used regular expressions in other languages, you just need to take a look.
A simple example is as follows: (The findall () function searches for strings and returns all matched substrings in the form of a list)
1 import re2 print (re. findall (r '\ d +', 'one1two2three3four4 ') # Here, the regular expression matches only the number 3 print (re. findall (R' \ bf [a-z] * ', 'which foot or hand fell fastest') # indicates that only words starting with f are output.
Urllib-Internet access
2. python of Version x can directly use import urllib for operations, but 3. python of Version x uses import urllib. request to perform the operation. {in the future, you will learn about network access operations for this topic}
A simple example is as follows:
1 from urllib. request import urlopen2 print (urlopen ("http://www.baidu.com"). read () # print Baidu's html source code
Datetime -- Date and Time
The datetime module provides methods for date and time processing. Supports Date and Time algorithms and supports formatting and output. {The topic study date and time will be listed in the future}
A simple example is as follows:
1 from datetime import date2 print (date. today () 3 now = date. today () 4 print (now. strftime ("% m-% d-% y, % A") # format output 5 birthday = date (1990,7, 7) 6 age = now-birthday # Time Processing 7 print (age. days)