What is a module:
A module is a collection of functions.
Modules are similar to Lego bricks, you use these modules to assemble a model, and then you can combine the modules with other modules into a new model.
Types of modules:
1. Built-in modules (Python comes with modules such as OS, file, etc.)
2, custom module, self-written module
3. Third-party modules
Import of modules:
Import modulefrom module.xx.xx import xxfrom module.xx.xx import xx as rename from module.xx.xx import *
Built-in Modules
1, OS to provide system-level operation
OS.GETCWD () Gets the current working directory, that is, the directory path of the current Python script work os.chdir ("DirName") changes the current script working directory, equivalent to the shell Cdos.curdir return the current directory: ('. ') Os.pardir Gets the parent directory string name of the current directory: (' ... ') Os.makedirs (' dirname1/dirname2 ') can generate a multi-level recursive directory Os.removedirs (' dirname1 ') if the directory is empty, then deleted, and recursively to the previous level of the directory, if 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 os.remove () Delete a file Os.rename ("Oldname", "newname") Rename File/directory Os.stat (' Path/filename ') Get File/directory information OS.SEP output operating system-specific path delimiter, win under "\ \", Linux for "/" OS.LINESEP output the current platform using the line terminator, win under "\t\n", Linux "\ N "os.pathsep output string to split file path Os.name output string indicates the current usage platform. Win-> ' NT '; Linux-> ' POSIX ' Os.system ("Bash command") runs a shell command that directly displays the Os.environ get system environment variable Os.path.abspath (PATH) Return path normalized absolute path Os.path.split (path) splits 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. A second element of Os.path.split (PATH) os.path.exists (Path) If path exists, returns true if path does not exist, return Falseos.path.isabs (PATH) If path is an absolute path, return Trueos.path.isfile (PATH) If path is a file that exists, 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 last modified time of the file or directory to which path is pointing
2. SYS is used to provide an interpreter-related operation
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 Sys.stdout.write (' please: ') val = Sys.stdin.readline () [:-1]
3, Hashlib
Mport Hashlib # ######## MD5 ######## hash = HASHLIB.MD5 () hash.update (' Shuaige ') print hash.hexdigest () >>> Impor T hashlib>>> hash = HASHLIB.MD5 () >>> hash.update (' Shuaige ') >>> print hash.hexdigest () 37d2b9990df5a6843caf19352fee42a6# ######## SHA1 ######## hash = HASHLIB.SHA1 () hash.update (' Shuaige ') print Hash.hexdigest () >>> hash = HASHLIB.SHA1 () >>> hash.update (' Shuaige ') >>> print Hash.hexdigest () fdb58cf91e7291b67815440281e4154e87747b68# ######## sha256 ######## hash = hashlib.sha256 () Hash.update (' Shuaige ') print hash.hexdigest () >>> hash = hashlib.sha256 () >>> hash.update (' Shuaige ') >>> print hash.hexdigest () 0dc6e2b03447ac1fde5a8ae5f9d609b2b37f26f5c8aeec5d244dde6184fde90d# ######## sha384 ######## hash = hashlib.sha384 () hash.update (' Shuaige ') print hash.hexdigest () >>> hash = hashlib.sha384 ( ) >>> hash.update (' Shuaige ') >>> print hash.hexdigest () 6dd79c38dc27c8f69411c2e77face2209606e08702fcbe7c5f73bb9e6a9ef1f58890156604ad6c71581dc5b6f7aea85e# ######## sha512 ######## hash = hashlib.sha512 () Hash.update (' Shuaige ') print hash.hexdigest () >>> hash = hashlib.sha512 () >>> hash.update (' Shuaige ') >>> Print hash.hexdigest () E9c94882cb0d9c61919f4d4c539a8bafe5f5a0708d214fbd50343c1e96a01ebb732883d0b0b36bff1e542cff69071395f511650944561807488700c71 fb06338
Although the above encryption algorithm is still very strong, but the time has the flaw, namely: through the collision library can reverse the solution. Therefore, it is necessary to add a custom key to the encryption algorithm to do encryption.
hash = hashlib.md5 (' 898oafs09f ') hash.update (' Shuaige ') print hash.hexdigest ()------------------------------------- ---------------------------------------------->>> hash = hashlib.md5 (' 898oafs09f ') # Add the custom information here and then encrypt >>> hash.update (' Shuaige ') >>> print hash.hexdigest () 6d1233c4e14a52379c6bc7a045411dc3
There are also powerful encryption methods: Python also has an HMAC module that internally creates key and content for us to process and then encrypt
Import Hmach = hmac.new (' Shuaige ') h.update (' Hello Laoshi ') print h.hexdigest ()
4. JSON and Pickle
Two modules for serialization
JSON, used to convert between string and Python data types
Pickle for conversion between Python-specific types and Python data types
The JSON module provides four functions: dumps, dump, loads, load
The Pickle module provides four functions: dumps, dump, loads, load
The JSON dumps converts the data type to a string dump to convert the data type into a string and store it in a file loads convert the string to a data type load opens the file to a data type from a string
Pickle
Now there is a scene in the data exchange between different devices very low way is to pass files, dumps can directly send the memory of server A to other servers, such as B server,In many scenarios, if you use pickle, then the A and B programs are Python programs this is not realistic, many times are different programs between memory exchange how to do? The use of JSON (and HTML-like language) and josn can dump the results more readable, then someone asked, that also use pickle do what not directly with Josn, is such josn can only be used to serialize the data type (list, dictionary, list, string, number,), such as date format, class Object! Josn will die. Why can't he serialize the above stuff? Because Josn is cross-lingual!
5, Configparser
Used to operate on a specific configuration, the name of the current module is changed to Configparser in the Python 3.x version.
# note 1; Note 2 [SECTION1]K1 = v1k2:v2 [section2]k1 = v1import configparser config = configparser.configparser () config.read (' i.cfg ') # ########## Read ########## #secs = config.sections () #print secs#options = config.options (' group2 ') #print options #item_ List = Config.items (' group2 ') #print item_list #val = config.get (' group1 ', ' key ') #val = Config.getint (' group1 ', ' key ') # # # ######## overwrite ########## #sec = config.remove_section (' group1 ') #config. Write (Open (' I.cfg ', "w") #sec = Config.has_ Section (' Shuaige ') #sec = config.add_section (' Shuaige ') #config. Write (Open (' I.cfg ', "w")) #config. Set (' Group2 ', ' K1 ', 11111) #config. Write (Open (' I.cfg ', "w") #config. Remove_option (' group2 ', ' age ') #config. Write (' I.cfg ', "w" ))
Custom Modules
Custom modules are usually written by themselves. A. py file can also be a folder
But the normal folder is not a module must have __int__ directory is the module
Open source Module
There are two ways to download the installation:
Integrated installation via third-party
Yum Pipapt-get ...
Install via source
Download source code to extract the source code into the directory compiled source python setup.py build installation source python setup.py install
Note: When using source installation, you need to use the GCC compilation and Python development environment
Not to be continued ...
Python Road "fourth": module