Python common modules are divided into two sections, respectively, the following modules
- Time Module
- Random module
- Hashlib Module
- OS Module
- SYS module
- Logging module
- Serial Number Module
- Configparser Module
- Re module
Time Module
In Python, there are usually three ways to represent time: timestamp, tuple (struct_time), formatted time string:
1) Timestamp: Typically, a timestamp represents an offset that is calculated in seconds, starting January 1, 1970 00:00:00. We run "type (Time.time ())" and return the float type.
(2) formatted time string (format string): ' 1988-03-16 '
(3) Tuple (struct time): structtime tuple total 9 elements Total nine elements: (year, month, day, time, minute, second, week of the year, Day of the year, etc.)
"' 1) Timestamp import time time.time () #返回当前时间戳
2) Time string Time.strftime ('%y-%m-%d%x ')
3) Time tuple time.localtime () ""
Conversion of several time forms
Timestamp <----> structured Time:
localtime/gmtime mktimetime.localtime (360024) time.gmtime (360024) time.mktime (Time.localtime ()) string time <----> Structured time: Strftime/strptimetime.strftime ("%y-%m-%d%x", Time.localtime ()) Time.strptime ("2017-03-16","%y-%m-%d" )
Random module
Importrandomrandom.random ()#decimals greater than 0 and small fish 10.17447943250177422Random.randint (1,5)#integer greater than or equal to 1 and less than or equal to 53Random.randrange (1, 3)#integers greater than or equal to 1 and less than 3Random.choice ([2,' at', [5,6]])#1 or 23 or [4,5]Random.sample ([1,' at', [4,5]],2]#any two combinations of list elementsRandom.uniform (5,8)#decimals greater than 5 and less than 8Random.shuffle (object)#Shuffle OrderGenerate random numbersImportRandomdefv_code (): Code="' forIinchRange (5): Num= Random.randint (0,9) Alf= Chr (Random.randint (65,90)) TMP= Chr (Random.randint (91,107)) Add=Random.choice ([num,alf,tmp]) code="'. Join ([Code,str (add)])returnCodePrint(V_code ())
Hashlib Module
This module provides a common digest algorithm, such as MD5,SHA1, etc.
Abstract the algorithm is also called hash algorithm and hashing algorithm. It uses a function to convert any length of data into a fixed length data string (usually represented by a 16-binary string).
ImportHASHLIBMD5=hashlib.md5 () md5.update ('How do I use the md5 in Python hashlib?')Print(Md5.hexdigest ()) d26a53750bc40b38b65a520292f69306 if the amount of data is large, you can call Update () multiple times in chunks, and the result is the same as the final calculation MD5=hashlib.md5 () md5.update ('How to use MD5 in') Md5.update ('python hashlib?')Print(Md5.hexdigest ()) d26a53750bc40b38b65a520292f69306
Although MD5 encryption, but not salt-added password is easy to be anti-hacker, so some people based on the user's user name to re-encrypt, this encryption is called add salt.
OS Module
An OS module is an interface that interacts with the operating system
OS.GETCWD () Gets the current working directory, which is the directory path of the current Python script work os.chdir ("dirname") To change the current script working directory, equivalent to the shell under Cdos.curdir return to the current directory: ('.') Os.pardir Gets the parent directory string name of the current directory: ('..') Os.makedirs ('dirname1/dirname2') to generate a multi-level recursive directory Os.removedirs ('dirname1'If the directory is empty, it is deleted and recursively to the previous level of the directory, if it is also empty, then delete, and so on Os.mkdir ('dirname') to generate a single-level directory, equivalent to the shell mkdir Dirnameos.rmdir ('dirname'Delete the single-level empty directory, if the directory is not empty can not be deleted, error, equivalent to the shell rmdir Dirnameos.listdir ('dirname'lists all files and subdirectories under the specified directory, including hidden files, and prints a list of os.remove () deletes a file Os.rename ("oldname","newname") Rename File/Catalog Os.stat ('Path/filename') Get File/directory information OS.SEP output operating system-specific path delimiter, win under"\\", under Linux for"/"os.linesep Output The line terminator used by the current platform, win under"\t\n", under Linux for"\ n"os.pathsep output is used to split the file path of the string win under;, Linux is: The Os.name output string indicates the current use of the platform. Win-'NT'; Linux->'POSIX'Os.system ("Bash Command"run the shell command to directly display the Os.environ get system environment variable Os.path.abspath (PATH) return path normalized absolute path Os.path.split (path) Split path into directory and file name two tuples return Os.path.dirname (path) to the directory where path is returned. In fact, the first element of Os.path.split (path) os.path.basename returns the last file name of path. If path ends with a/or \, then a null value is returned. The second element of Os.path.split (path), os.path.exists (path), returns True if path exists, or if path does not exist, returns Falseos.path.isabs if path is an absolute path, Returns Trueos.path.isfile (path) If path is an existing file and returns True. Otherwise, return Falseos.path.isdir (path) True if path is a directory that exists. Otherwise return Falseos.path.join (path1[, path2[, ...]) When multiple paths are combined, the parameters before the first absolute path are ignored Os.path.getatime (path) returns the last access time of the file or directory to which path is pointing os.path.getmtime (path) Returns the size of the file or directory to which path was last modified Os.path.getsize (path) returned path
SYS module
SYS.ARGV command line argument list, the first element is the program itself path Sys.exit (n) exits the program, exit normally (0) sys.version Gets the version information of the Python interpreter Sys.maxint the largest int value Sys.path returns the search path for the module, using the value of the PYTHONPATH environment variable when initializing Sys.platform Returns the operating system platform name
Python common modules on the article