10 "Common modules:
1>os module:
OS.GETCWD (): Gets the current working directory, which is the directory path of the current Python script work;
Os.chdir ("DirName"): Changes the current script working directory, equivalent to the shell under the CD;
Os.curdir: Returns the current directory: ('. ') )
Os.pardir: Gets the parent directory string name of the current directory: (' ... ') )
Os.makedirs (' dirname1/dirname2 '): Multi-level recursive directory can be generated;
Os.removedirs (' dirname1 '): If the directory is empty, then delete, and recursively to the previous level of the directory, if also empty, then delete, and so on;
Os.mkdir (' dirname '): Generates a single-level directory, equivalent to mkdir dirname in the shell;
Os.rmdir (' dirname '): Delete the single-level directory, if the directory is not empty, delete;
Os.listdir (' dirname '): Lists all files and subdirectories under the specified directory;
Os.remove: Delete a file;
Os.rename ("Oldname", "NewName"): Renaming files/directories;
Os.stat (' Path/filename '): Get file/directory information;
OS.SEP: Output operating system-specific path delimiter, win under "\ \", Linux for "/";
OS.LINESEP: Output The line terminator used by the current platform, win under "\t\n", Linux "\ n";
OS.PATHSEP: Outputs the string used to split the file path;
Os.name: The output string indicates the current use of platform,win-> ' NT '; Linux-> ' POSIX ';
Os.system ("Bash Command"): Run shell command, direct display;
Os.popen ("Bash Command"). Read (): Stores the results of the shell command execution;
Os.environ: Obtaining system environment variables;
Os.path.abspath (PATH): Returns the absolute path normalized by path;
Os.path.split (PATH): partition path into directory and file name two tuples returned;
Os.path.dirname (PATH): Returns the directory of path, which is actually the first element of os.path.split (path);
Os.path.basename (PATH): Returns the last file name of path
Os.path.exists (PATH): Returns True if path exists, or false if path does not exist;
Os.path.isabs (PATH): Returns True if path is an absolute path;
Os.path.isfile (PATH): Returns True if Path is an existing file, false otherwise;
Os.path.isdir (PATH): Returns True if Path is an existing directory, otherwise false;
Os.path.join (path1[,path2[, ...]): Combine multiple paths to return, the first absolute path;
Os.path.getatime (PATH): Returns the last access date of the file or directory to which path is pointing;
Os.path.getmtime (PATH): Returns the last modified date of the file or directory to which path is pointing;
2>sys module:
SYS.ARGV: Command line argument list, the first element is the program itself;
Sys.exit (n): Exit 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 of the module;
Sys.platform: Returns the name of the operating platform;
Sys.stdout.write (' please: '): Used as a progress bar;
val = Sys.stdin.readline () [:-1]
3>shutil module: Advanced files, folders, compression package processing module;
Shutil processing of the compressed packet is done by calling the ZipFile and tarfile two modules;
Shutil.copy.fileobj (Fsrc,fdst[,length]): Copy the contents of the file to another file, can be part of the content;
Shutil.copyfile (SRC,DST): Copy file;
Shutil.copymode (SRC,DST): Copy only permission, content, group, user are unchanged;
Shutil.copystat (SRC,DST): Information about the status of the copy, including mode bits,atime,mtime,flags;
Shutil.copy (SRC,DST): Copy files and permissions;
Shutil.copy2 (SRC,DST): Copy files and status information;
Shutil.ignore_patterns (*patterns):
Shutil.copytree (Src,dst,symlinks=false,ignore=none): recursive first copy file;
Shutil.rmtree (Path[,ignore_errors[,onerror]): To remove files recursively;
Shutil.move (SRC,DST): To move the file recursively;
Shutil.make_archive (Base_name,format,....): Creates a compressed package and returns the file path;
Base_name: The file name of the compressed package, it can also be the path of the compressed package, but only when the file name is saved to the current directory, otherwise saved in the specified path;
Format: Compressed package type, "Zip", "tar", "Bztar", "Gztar";
4>shelve module: A simple k,v module that persists memory data through a file and can persist any python that pickle can support
Data format;
Persistence:
Read:
5>xml Processing module:
XML is a protocol that implements data exchange between different languages or programs, much like JSON, but JSON is simpler to use, but when JSON is not yet available, you can only use XML, and many of the systems of traditional companies, such as the financial industry, are mostly XM L;xml is to differentiate the data structure through the <> node;
XML document content:
XML node Traversal:
XML Node Modification:
XML node deletion:
6>configparser (name in 3) module:----"Configparser (name in 2)
Definition: Used to generate and modify common configuration documents;
Write configuration file:
To read a configuration file:
Delete some of the contents of the configuration file:
Add the contents of the configuration file:
Overwrite configuration file contents:
To delete content under an element in a configuration file:
7>hashlib module: For cryptographic related operations, 3 in place of the MD5 module and SHA module, mainly provides SHA1,SHA224,SHA256,SHA384,SHA512,MD5 algorithm; input , the result of encryption is the same, and cannot be reversed after encryption;
MD5 Encryption Example:
SHA512 Encryption Example:
With respect to encryption, Python also has an HMAC module that internally creates key and content for us to process and then encrypt;
8>subprocess module: Interacting with the operating system, executing commands and executing scripts, instead of Subprocess.run () in Os.system;3, 2 is Subprocess.call ();
Execute command with no parameters:
Execute command with parameters:
Get the real result of executing the command:
Check execution status return code, return 0 if correct, throw exception if error:
The commands for terminal input can be divided into two types:
Input to get output: such as: ifconfig
Input to an environment that relies on re-entry, such as: Python
Examples of commands that require interaction: (Create another child process in the parent process)
9>logging module:
Many programs have logging requirements, and the log contains information that has both normal program access logs, and possibly errors, warnings and other information output, Python's logging module provides a standard log interface, we can store various formats of the log, logging log can be divided into Debug (), info (), warning (), error () and critical () 5 levels;
Print log information:
Write log to File: (only this level and above levels will be stored after the set level)
Add time to the log:
Print the log in both the screen and the log file:
Python's Module II