Sys
SYS module is an interface that interacts with the Python interpreter
SYS.ARGV command line argument list, the first element is the path of the program itself
Sys.exit (n) exit program, Exit normally on exit (0), error exit Sys.exit (1)
Sys.version get version information for Python interpreter
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
Os
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") changes the current script working directory, equivalent to the shell CD
Os.curdir returns the current directory: ('. ')
Os.pardir Gets the parent directory string name of the current directory: (' ... ')
Os.makedirs (' dirname1/dirname2 ') can generate multi-level recursive directories
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 empty directory, if the directory is not empty can not be deleted, error, equivalent to the shell rmdir dirname
Os.listdir (' dirname ') lists all files and subdirectories in the specified directory, including hidden files, and prints as a list
Os.remove () In addition to 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 output is used to split the file path of the string win down for;, Linux for:
The Os.name output string indicates the current usage platform. Win-> ' NT '; Linux-> ' POSIX '
Os.system ("Bash command") runs a shell command that directly displays
Os.popen ("Bash Command"). Read () Run the shell command to get the result of the execution
Os.environ Getting system environment variables
Os.path.abspath (path) returns path normalized absolute path Os.path.split (path) splits path into directory and file name two tuples return os.path.dirname (path) directory that returns path
Os.path.exists (path) returns true if path exists, 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 a file that exists. otherwise returns false
Os.path.isdir (path) returns True if path is a directory that exists. otherwise returns false
Os.path.join (path1[, path2[, ...]) returns a combination of multiple paths, and 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
Os.path.getsize (path) returns the size of path
Re
A regular expression used to match a string.
Metacharacters
. Any one character other than the line break
? Match 0 to 1 arbitrary characters
(+) match 1 to any number of characters
(*) match 0 to any number of characters
{, num2} configuration 0 to num2 {} The preceding character
{num1,num2} matches num1 to num2 {} before that character
{NUM1,} matches num1 to multiple {} characters before
^ with ... Beginning
$ to ... End
[A-z] character set, which allows to match any one character in a-Z, in [], except ^ represents non, \ Outside, other metacharacters no longer have special meaning
\ followed by metacharacters to remove meta-character special functions
> \d matches any decimal number equal to [0-9]
> \d matches any non-numeric character equivalent to [^0-9]
> \s matches any non-whitespace character equivalent to [\t\n\r\f\v]
> \s matches any non-whitespace character equivalent to [^ \t\n\r\f\v]
> \w matches any alphanumeric character, equivalent to [a-za-z0-9]
> \w matches any non-alphanumeric character equivalent to [^a-za-z0-9]
> \b Matches a word boundary
> R Original Characters
Re.findall (' kernel ', ' kernelhjghjgbhjhjkerneljhjh ') finds a string in the following string that matches the preceding rule and returns it as a list
Re.match (pattern,string,flag=0) only starts the match from the beginning of the string, the configuration succeeds, it returns a Match object object, and must be reached by the method that called the match object object to get the value
> Start () returns the position where the match started
> End () returns the position of the end of the match
> span () returns a tuple containing the position of the match (start, end)
> Group () returns a string that matches successfully
> Groups () Gets the grouped results that are matched in the model
> groupdict () ibid., in the form of key:value R = Re.match (' H (? p<name>\w+) ') is Key=name value=as
Re.serach () is similar to match (), where a match can be started from anywhere in the string, but only once
Re.sub (Pattern,repl,string,max) replacement, based on the pattern configuration, replaces the matching result with Repl,max to indicate the maximum number of replacements
Similar to RE.SUBN () and Re.sub (), the difference is that the number of matches is also returned
Re.split () split
Re.compile () encapsulates a matching rule into an object that is more efficient in scenarios where repeated occurrences of the same rule match
Time
Time Processing Module
Several ways to convert time modules
Print (Time.time ()) timestamp
Print (Time.strftime ("%y-%m-%d%x")) formatted time string
Print (Time.localtime ()) local time zone Struct_time
Print (Time.gmtime ()) UTC time Zone Struct_time
Time stamp conversion to structured time
Now_time = Time.time () timestamp
Print (Now_time)
Struct_time = Time.localtime (now_time) timestamp converted to structured time
Print (Struct_time)
Structured time is converted to timestamp
Struct_time = Time.localtime (Time.time ()) Structured time
Print (Struct_time)
Now_time = Time.mktime (struct_time) structured time converted to timestamp
Print (Now_time)
Structured time converted to string time
Struct_time = Time.localtime (Time.time ()) Structured time
Print (Struct_time)
Str_time = Time.strftime ("%y-%m-%d", Struct_time) structured time converted to string time
Print (Str_time)
Convert string time to structured time
Str_time = "2018-04-17" string time
Struct_time = Time.strptime (Str_time, "%y-%m-%d") string time converted to structured time
Print (Struct_time)
Timedate
Date Processing Module
Timedate.date.today () Output date
Timedate.date.fromtimestamp () receive timestamp, convert to date
Timedate.current_time () Output date time millisecond value
Timedate.current_time.timetuple () Converts a datetime millisecond value to Struct_time
Timedate.current_time.replace () Replace date
Json
Can only be applied to Python's basic data types, cross-language
Json.loads () takes a string and converts it to a Python data type
Json.load () reads a string from the culture and translates it into a Python data type
Json.dumps () receives a Python data type. Convert to String
Json.dump () receives a Python data type, converts it into a string, writes to a file
Pickle
All data types for Python, but only for Python
Pickle.loads () bytes received, converted to Python data type
Pickle.load () Read bytes from culture, convert to Python data type
Pickle.dumps () receives a Python data type. Convert into bytes
Pickle.dump () receives a Python data type, converts to bytes, writes to a file
Logging
Log processing module
Level of Log
By default, Python's logging module prints the logs to standard output and only displays logs that are greater than or equal to the warning level, indicating that the default logging level is set to warning (log level level critical > ERROR > WARNING > INFO > DEBUG)
Configuration of the log
Logging.basicconfig (level=logging. DEBUG,
format= '% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s ',
datefmt= '%a,%d%b%Y%h:% M:%s ',
filename= '/tmp/test.log ',
filemode= ' W ')
Logging.debug (' Debug message ')
Logging.info (' info Message ')
logging.warning (' warning message ')
Logging.error (' error message ')
Logging.critical (' critical Message ')
Configuration parameters
The Logging.basicconfig () function can be used to change the default behavior of the logging module through specific parameters such as:
FileName: Creates a filedhandler with the specified filename so that the log is stored in the specified file.
FileMode: File is opened by using this parameter when filename is specified, and the default value is "a" and can be specified as "W".
format Specifies the log display formats used by handler.
DATEFMT: Specifies the date time format.
Level: Set the log levels for Rootlogger (which will explain the concepts behind)
Stream: Creates a streamhandler with the specified stream. You can specify the output to Sys.stderr,sys.stdout or the file (F=open (' Test.log ', ' W ')), and the default is Sys.stderr. If you list both the filename and stream two parameters, the stream parameter is ignored.
formatting strings that may be used in the format parameter:
% (name) s logger name
% (Levelno) s log level in digital form
% (levelname) s log level in text form
% (pathname) s calls the full pathname of the module of the log output function and may not have
% (filename) s The file name of the module that called the log output function
% (module) s call the module name of the log output function
% (FuncName) s Call the function name of the log output function
% (Lineno) d The line of code where the statement of the log output function is called
% (created) F current time, represented by the UNIX standard floating-point number representing the time
% (relativecreated) d when the log information is output, the number of milliseconds since logger was created
% (asctime) s The current time in string form. The default format is "2003-07-08 16:49:45,896". The comma is followed by milliseconds
% (thread) d-Path ID. Probably not.
% (threadname) s thread name. Probably not.
% (process) d process ID. Probably not.
% (message) s user-output message
Logger Object Configuration
Logger = Logging.getlogger (' kernel ') # Create logging Object
Logger.setlevel (logging. DEBUG) # Specifies the Globally processed message level (the global processing message level is higher or equal to the local message processing level)
ch = logging. Streamhandler () # screen Flow
Ch.setlevel (logging. DEBUG) # Specifies the level of locally processed messages
FH = logging. Filehandler ("Access.log") # File stream
Fh.setlevel (logging. WARNING) # Specifies the level of locally processed messages
Formatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ') # Create time format
Ch.setformatter (Formatter) # set screen stream time format
Fh.setformatter (Formatter) # Set text flow time format
Logger.addhandler (CH) # Add a screen stream object to the logger
Logger.addhandler (FH) # Add a text flow object to Logger
Logger.debug (' Debug message ')
Logger.info (' info message ')
Logger.warn (' Warn message ')
Logger.error (' Error message ')
Logger.critical (' critical message ')
Hashlib
Cryptographic modules
obj = hashlib.md5() # 创建hashlib对象obj = hashlib.md5(bytes(‘kernel‘,encoding=‘utf-8‘)) # 创建hashlib对象并加言obj.update(bytes(‘hello‘,encoding=‘utf-8‘)) # 加密ret = obj.hexdigest() # 获取密文
Configparser
Manipulating configuration file Modules
import configparsercfg = configparser.ConfigParser() # 创建configparser对象cfg.read(‘conf‘,encoding=‘utf-8‘) # 读取配置文件secs = cfg.sections() # 获取所有节点print(secs)options = cfg.options(secs[0]) # 获取指定节点的所有keyprint(options)item = cfg.items(secs[0]) # 获取指定节点的键值对组合print(item)val = cfg.get(secs[0],options[0]) # 获取指定节点下的指定key的值print(val)sec = cfg.remove_section(secs[0]) # 删除指定节点cfg.write(open(‘conf‘,‘w‘))print(sec)sec = cfg.has_section(‘jiedian1‘) # 检查是否存在该节点print(sec)sec = cfg.add_section(‘jiedian1‘) # 添加该节点cfg.write(open(‘conf‘,‘w‘))print(sec)cfg.set(secs[0],options[0],‘111‘) # 修改指定节点下指定key的值cfg.write(open(‘conf‘,‘w‘))cfg.remove_option(secs[0],options[0]) # 删除指定节点下指定键值对cfg.write(open(‘conf‘,‘w‘))
Xml
manipulating XML file Modules
From Xml.etree import ElementTree as Etfrom xml.dom import minidom# Modify XML "" " There are two ways to parse XML 1. Parses a string to parse a string into an XML object, and root refers to the root node of the XML file Str_xml = open (' Xo.xml ', ' R '). Read () root = ET. XML (Str_xml) 2. Parse file method gets the root node of the xml file tree = Et.parse ("xo.xml") root = Tree.getroot () "" "# Traverse all the contents of the XML ET = Et.parse (' Conf.xml ') root = Et.getroot () # Gets the root node print (root) print (Root.tag) # Top level label for child in Root: # traverse the second level of the XML document print (' + Child.tag,chil D.attrib) # Tag name and label properties for the second-level node for I in the Child: # traverse the third layer of the XML document print (' + i.tag,i.attrib) # Tag name and label properties for the third-level node Print (' + str (i.text) #) # Third-level node properties # traverse the specified node of XML for node in Root.iter (' year '): # Traverse all the year nodes of XML print (Node.tag, Node.text) # node label name and content # node label name and content and modify node contents for nodes in Root.iter (' year '): # Traverse all the year nodes of XML print (Node.tag, node.text) # The label name and content of the node new_year = Int (node.text) + 1 # Increases the contents of the year node by 1 node.text = str (new_year) node.set (' name ', ' kernel ') # Set properties and Values Node.set (' Age ', ' Max ') del node.attrib[' name '] # Delete attribute # Delete node for country in Root.findall (' CouNtry '): # Traverse all country nodes under data rank = Int (country.find (' rank '). Text) # Gets the contents of the rank node under each country node if rank > 50: Root.remove (country) # Delete the specified country node "" "There are two ways to save an XML file 1. Parse string Way tree = ET. ElementTree (Root) tree.write ("Newnew.xml", encoding= ' Utf-8 ') 2. parsing file mode Tree.write ("New.xml", encoding= ' Utf-8 ') "" "# Create an XML file # way a root = ET. Element ("famliy") Son1 = ET. Element (' son ', {' name ': ' Older Son '}) # Create a big son node Son2 = ET. Element (' son ', {"name": ' Two sons '}) # Create two sons node Grandson1 = ET. Element (' grandson ', {' name ': ' Great Grandson '}) # Create two grandchildren in the older son grandson2 = ET. Element (' grandson ', {' name ': ' Two grandson '}) Son1.append (grandson1) # Add grandson to son node Son1.append (grandson2) Root.append (son1) # Add the son to the root node root.append (son1) tree = ET. ElementTree (Root) tree.write (' Oooo.xml ', encoding= ' utf-8 ', short_empty_elements=false) # way Two root = ET. Element ("famliy") Son1 = root.makeelement (' son ', {' name ': ' Big Son '}) # Create a big son node Son2 = root.makeelement (' son ', {"name": ' Two sons '} # Create two sons node Grandson1 = root.makeelement (' grandson ', {' name ': ' Great Grandson ') # Create two grandchildren in the older son grandson2 = root.maKeelement (' grandson ', {' name ': ' Two grandson '}) Son1.append (grandson1) # Add grandson to son node Son1.append (grandson2) Root.append (son1) # Add the son to the root node root.append (son1) tree = ET. ElementTree (Root) tree.write (' Oooo.xml ', encoding= ' utf-8 ', Short_empty_elements=false) # way Three root = ET. Element ("famliy") Son1 = root. subelement (' son ', {' name ': ' Big Son '}) # Create a big son node Son2 = root. subelement (' son ', {"name": ' Two sons '}) # Create two sons node Grandson1 = root. subelement (' grandson ', {' name ': ' Great Grandson ') # two grandchildren created in the older son grandson2 = root. subelement (' grandson ', {' name ': ' Two grandson '}) Son1.append (grandson1) # Add grandson to son node Son1.append (grandson2) root.append (Son1) # Add your son to the root node root.append (son1) tree = ET. ElementTree (Root) tree.write (' Oooo.xml ', encoding= ' utf-8 ', short_empty_elements=false) # Short_empty_elements=false Represents the format of the control element if the value is false, there is no content, they are as a separate closed label, otherwise they will be fired as a pair of start/end tags # way four native saved XML default no indentation, if you want to set indentation, you need to modify the Save Way def prettify ( Elem): "" Converts the node to a string and adds indentation. "" "rough_string = et.tostring (elem, ' utf-8 ') reparsed = minidom.parsestring (rough_string) return reparsed.topRettyxml (indent= "\ t") root = ET. Element ("famliy") Son1 = ET. Element (' son ', {' name ': ' Older Son '}) # Create a big son node Son2 = ET. Element (' son ', {"name": ' Two sons '}) # Create two sons node Grandson1 = ET. Element (' grandson ', {' name ': ' Great Grandson '}) # Create two grandchildren in the older son grandson2 = ET. Element (' grandson ', {' name ': ' Two grandson '}) Son1.append (grandson1) # Add grandson to son node Son1.append (grandson2) Root.append (son1) # Add the son to the root node Root.append (son1) Raw_str = prettify (root) F = open ("Xxxoo.xml", ' W ', encoding= ' Utf-8 ') f.write (RAW_STR) F.close ()
Subprocess
Execute System Command Module
Pager
> Execute command, return status code
> ret = subprocess.call (["LS", "-l"], Shell=false)
> ret = Subprocess.call ("Ls-l", Shell=true)
Check_call
> Executes the command, returns 0 if the execution status code is 0, or throws an exception
> subprocess.check_call (["LS", "-l"])
> Subprocess.check_call ("Exit 1", shell=true)
Check_output
> Executes the command, if the status code is 0, returns the execution result, otherwise throws an exception
> subprocess.check_output (["echo", "Hello world!"])
> Subprocess.check_output ("Exit 1", shell=true)
Subprocess. Popen (...)
> for executing complex system commands
> Parameters:
> Args:shell command, which can be a string or sequence type (for example: list, tuple)
> bufsize: Specifies buffering. 0 unbuffered, 1 row buffer, other buffer size, negative system buffering
> stdin, stdout, stderr: Indicates the standard input, output, and error handles of the program, respectively
> PREEXEC_FN: Valid only on UNIX platforms for specifying an executable object (callable object) that will be called before the child process runs
> CLOSE_SFS: Under the Windows platform, if Close_fds is set to true, the newly created child process will not inherit the input, output, and error pipes of the parent process.
> CLOSE_SFS: Under the Windows platform, if Close_fds is set to true, the newly created child process will not inherit the input, output, and error pipes of the parent process.
> CLOSE_SFS: Under the Windows platform, if Close_fds is set to true, the newly created child process will not inherit the input, output, and error pipes of the parent process, so the Close_fds cannot be set to true while redirecting the child process's standard input, Output with error (stdin, stdout, stderr).
> Shell: Ibid.
> CWD: Used to set the current directory of child processes
> env: The environment variable used to specify the child process. If env = None, the environment variables of the child process are inherited from the parent process.
> universal_newlines: Different system line breaks differ, True----agree to use \ n
> Startupinfo and Createionflags are only valid under Windows and will be passed to the underlying CreateProcess () function to set some properties of the child process, such as: the appearance of the main window, the priority of the process, etc.
import subprocess"""终端输入的命令分为两种:输入即可得到输出,如:ifconfig输入进行某环境,依赖再输入,如:python"""
Execute common commands
Ret1 = subprocess. Popen (["mkdir", "T1"])
Ret2 = subprocess. Popen ("mkdir T2", shell=true)
Create a folder on the specified directory
obj = subprocess. Popen ("mkdir T3", Shell=true, cwd= '/home/dev ',)
Environment-dependent commands
obj = subprocess. Popen (["Python"], stdin=subprocess. PIPE, Stdout=subprocess. PIPE, Stderr=subprocess. PIPE, Universal_newlines=true)
Obj.stdin.write ("print (1) \ n")
Obj.stdin.write ("Print (2)")
Out_error_list = Obj.communicate ()
Print (out_error_list)
obj = subprocess. Popen (["Python"], stdin=subprocess. PIPE, Stdout=subprocess. PIPE, Stderr=subprocess. PIPE, Universal_newlines=true)
Out_error_list = Obj.communicate (' Print ("' Hello ') ')
Print (out_error_list)
Python Common Module Summary