python-Common Modules
Python module
The module is the highest-level Python program organizational unit that encapsulates program code and data for reuse.
In practice, modules often correspond to Python program files. The essence is to use some code to implement certain functions of the collection
This collection can be a. py file, or it can be a package (in a folder that has a. PY entry file)
First, the Import module
Import Module
From module.xx import xx
From module.xx import xx as rename
From module.xx Import *
Import a py file, the interpreter interprets the file
Import a Package
Import module based on path Sys.path if you have this path, you can import it directly
If Sys.path has the desired path, pass sys.path.append (' path ')
Second, open source module
Download installation
1. Yum, Pip, Apt-get
2. Source code compilation Installation: Python setup.py build python setup Install
Three, common modules
1. OS module # used as a system-level work
Os.popen (' id '). read () # Execute system command Get return result
Os.system () # Get Return status returned cannot intercept
Os.name # Return to System platform Linux/unix user is ' POSIX '
Os.getenv () # Read environment variable
Os.putenv () # Setting environment variables
OS.GETCWD () # Current working path
Os.chdir () # Change the current working directory
Os.walk ('/root/') # recursive path
File processing
Mkfifo ()/mknod () # Create a named pipe/create a file system node
Remove ()/unlink () # Delete file
Rename ()/renames () # Rename File
*stat () # Return file information
Symlink () # Create Symbolic Links
Utime () # Update timestamp
Tmpfile () # Create and open (' W+b ') a new temporary file
Walk () # Traverse all filenames under the directory tree
Directory/Folder
ChDir ()/fchdir () # Change current working directory/change current working directory with a file descriptor
Chroot () # Changes the root directory of the current process
Listdir () # list files for the specified directory
GETCWD ()/getcwdu () # Returns the current working directory/function same, but returns a Unicode object
mkdir ()/makedirs () # Create a directory/create a multi-level directory
RmDir ()/removedirs () # Delete directory/delete multi- level directory
Access/Permissions
Saccess () # Verify permission mode
chmod () # Change permission mode
Chown ()/lchown () # Changes the owner and GroupID functions the same, but does not track links
Umask () # Set default permission mode
File descriptor Operations
Open () # Underlying operating system open (for robustness, use the standard built-in open () function)
Read ()/write () # Read/write data according to file descriptor read part of file by size
DUP ()/dup2 () # Copy file description symbol/function same, but copy to another file descriptor
Device number
Makedev () # Create an original device number from the major and minor device numbers
Major ()/minor () # Get the Major/minor device number from the original device number
Os.path Module
Os.path.expanduser (' ~/.ssh/key ') # full path to files in home directory
Separated
Os.path.basename () # Remove directory path, return file name
Os.path.dirname () # Remove file name, return directory path
Os.path.join () # Combine parts of a separate path name
Os.path.spllt () # Return (DirName (), basename ()) tuple
Os.path.splitdrive () # return (drivename,pathname) tuple
Os.path.splitext () # return (filename,extension) tuple
Information
Os.path.getatime () # Returns the last access time
Os.path.getctime () # Return file creation time
Os.path.getmtime () # Returns the last file modification time
Os.path.getsize () # Return file size (bytes)
Inquire
Os.path.exists () # Specifies whether the path (file or directory) exists
Os.path.isabs () # Specifies whether the path is an absolute path
Os.path.isdir () # Specifies whether the path exists and is a directory
Os.path.isfile () # Specifies whether the path exists and is a file
Os.path.islink () # Specifies whether the path exists and is a symbolic link
Os.path.ismount () # Specifies if the path exists and is a mount point
Os.path.samefile () # Two path names point to the same file
2. SYS module # provides interpreter-related operations
SYS.ARGV # command line argument list, the first element is the path of the program itself
Sys.exit (2) # Exit Script return status will be intercepted by try
Sys.exc_info () # Gets the exception class that is currently being processed
Sys.version # Get version information for Python interpreter
Sys.maxint # Maximum int value 9223372036854775807
Sys.maxunicode # maximum Unicode value
Sys.modules # Returns the system-imported module field, key is the module name, value is the module
Sys.path # Returns the search path of the module, using the value of the PYTHONPATH environment variable when initializing
Sys.platform # Returns the operating system platform name
Sys.stdout # Standard Output
Sys.stdin # Standard Input
Sys.stderr # Error Output
Sys.exec_prefix # Returns the location of the platform standalone Python file installation
Sys.stdin.readline () # reads a line from standard input
Sys.stdout.write ("a") # screen output a
3. Hashlib Module # for encryption-related operations
General Encryption
Import Hashlib
HASH_MD5 = HASHLIB.MD5 ()
Hash_md5.update (' admin ')
Print (Hash_md5.hexdigest ())
hash = hashlib.sha512 ()
Hash.update (' admin ')
Print Hash.hexdigest ()
#以上加密算法虽然依然非常厉害, but the time is flawed, namely: through the pool can be reversed. Therefore, it is necessary to add a custom key to the encryption algorithm to do encryption.
Add a custom key to do encryption
Import Hashlib
hash = hashlib.md5 (' 898oafs09f ')
Hash.update (' admin ')
Print hash.hexdigest ()
Super Cock Encryption-continue to encrypt the key and content we created
Import HMAC
h = hmac.new (' Wueiqi ')
H.update (' Hellowo ')
Print H.hexdigest ()
4. JSON and pickle modules #用于序列化数据
JSON, #json解决简单数据类型的序列换
Pickle, #能存储python的复杂数据类型
The JSON module provides four functions: dumps, dump, loads, load
The Pickle module provides four functions: dumps, dump, loads, load
Name_tra=json.dumps (' [1,2,3,4,5] ') #dunmps将序列化的数据放到内存
With open (' Json_tra ', ' WB ') as F_json:
F_json.write (Name_tra)
With open (' Json_tra1 ', ' WB ') as F_json: #dump直接将内存中序列化的数据写入文件
Json.dump (A,f_json)
Time_now=datetime.datetime.now ()
Pickle_mem=p.dumps (Time_now)
Pickle_load=p.loads (PICKLE_MEM)
5. Subprocess Module
Subprocess package to fork a child process and run an external program.
The subprocess package defines a number of functions that create child processes. Subprocess also provides some tools for managing standard streams and pipelines (pipe) to use text communication between processes.
Subprocess.call ()
Parent process waits for child process to complete
Return exit information (ReturnCode, equivalent to Linux exit code)
B=subprocess.call ([' ls ', '-l '])
B=subprocess.call ("Ls-l", Shell=true)
Shell=true, allows shell commands to be in string form.
Subprocess.check_call ()
Parent process waits for child process to complete
Returns 0
Check the exit information, and if ReturnCode is not 0, cite error subprocess. Calledprocesserror, this object contains the ReturnCode property, which can be try...except ... To check
Subprocess.check_call (["LS", "-l"])
Subprocess.check_call ("Exit 1", shell=true)
Subprocess.check_output ()
Parent process waits for child process to complete
Returns the output of the child process to the standard output
Check the exit information, and if ReturnCode is not 0, cite error subprocess. Calledprocesserror, which contains the ReturnCode property and the output property, outputs a result of the standard output, available try...except ... To check
6. Shuit Module #文件的复制移动
Shutil.copyfile (' data.db ', ' archive.db ') # Copy file
Shutil.move ('/build/executables ', ' installdir ') # move files or directories
7. Logging Module # Format logging
#通过logging. basicconfig function to configure the output format and mode of the log, the log will be written to the file
Logging.basicconfig (level=logging. DEBUG,
format= '% (asctime) s [line:% (Lineno) d]% (levelname) s% (message) s ',
datefmt= '%y/%m/%d%h:%m:%s ',
Filename= ' Myapp.log ',
Filemode= ' a ')
Logging.debug (' This is Debug message ')
Logging.info (' This is Info message ')
Logging.warning (' This is warning message ')
#logging. Basicconfig function Parameters:
#datefmt: Specify time format, same as Time.strftime ()
#filename: Specify the log file name
#filemode: The same as the file function, specify the open mode of the log file, ' W ' or ' a '
#level: Sets the logging level by default to logging. WARNING
#format: Specifies the format and content of the output, and format can output a lot of useful information, as shown in the previous example:
#% (asctime) s: Time to print logs
#% (levelname) S: Print log level name
#% (message) s: Print log information
#% (Levelno) S: Print the value of the log level
#% (Lineno) d: Print the current line number of the log
#% (pathname) s: Prints the path of the currently executing program, which is actually sys.argv[0]
#% (filename) S: Print the current execution program name
#% (funcName) s: Print the current function of the log
#% (thread) d: Print thread ID
#% (threadname) s: Print thread name
#% (process) d: Print process ID
#将日志同时写入文件, and print to the screen
Logging.basicconfig (level=logging. DEBUG,
format= '% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s ',
datefmt= '%y/%m%d%h:%m:%s ',
Filename= ' Myapp.log ',
Filemode= ' W ')
#打印到屏幕
console = logging. Streamhandler ()
Console.setlevel (logging. WARNING)
Formatter = logging. Formatter ('% (name) -12s:% (levelname) -8s% (message) s ')
Console.setformatter (Formatter)
Logging.getlogger (). AddHandler (console)
8. The random module is used to take the stochastic number
Random.choice ([' Apple ', ' pear ', ' banana ') # random fetch list one parameter
Random.sample (xrange (100), 10) # 10 are not repeatedly extracted
Random.randrange (3,7) #随机抽取整数范围不包括7
Random.random () # Random floating point number
9. Time DateTime Module
Timestamp #1970年1月1日之后的秒, i.e.: Time.time ()
Formatted string # 2014-11-11 11:11, i.e.: Time.strftime ('%y-%m-%d ')
Structured time # Tuple contains: year, day, week, etc. time.struct_time is: time.localtime ()
Import time
Time.time () # timestamp [floating point]
Time.localtime () [1]-1 # last month
Int (Time.time ()) # timestamp [whole S]
Time.strftime ('%y-%m-%d%x ') #格式化输出时间
Import datetime
Datetime.datetime.datetime.now () #现在的时间
Datetime.datetime.now ()-Datetime.timedelta (days=5,hours=3,seconds=3,minutes=2) #减时间
10, re module regular matching
#Pattern对象是一个编译好的正则表达式, the text can be matched by a series of methods provided by the pattern.
Pattern=re.compile (strpattern[, flag]):
Flag is a matching pattern, re. I|re. M means that it takes effect simultaneously.
Re. I (re. IGNORECASE): Ignore case
M (MULTILINE): Multiline mode, changing the behavior of ' ^ ' and ' $ '
Match (string[, pos[, Endpos]) | Re.match (pattern, string[, flags]): #结果匹配一次
#match (string[, pos[, Endpos]) attempts to match pattern from the POS subscript of string and returns a match object if the pattern is still matched at the end;
The default values for POS and Endpos are 0 and Len (string), and Re.match () flags specify a matching pattern when compiling pattern.
#re. Match (pattern, string[, flags]) cannot specify POS and Endpos values. So the match is at the beginning of the match.
Search (string[, pos[, Endpos]) | Re.search (pattern, string[, flags]): #结果匹配一次
#search (string[, pos[, Endpos]) attempts to match pattern from the POS subscript of string, and returns a match object if the pattern is still matched at the end;
If the match is not matched, the POS is added 1 after the match is attempted again, and none is returned until Pos=endpos is still not matched.
#re. Search (pattern, string[, flags]), to match any start bit
A= ' 321.423.432.432 33.43.5.42 1.2.443.34 255.52.53.255 2.2.2.2 3.3.3.3 '
Pattern=re.compile (R ' ([12]?\d{1,2}\.) {3} ([12]?\d{1,2})
Print Pattern.search (a). Group ()
Split (string[, Maxsplit]) | Re.split (Pattern, string[, Maxsplit]):
Returns a list after splitting a string by a substring that can be matched. The maxsplit is used to specify the maximum number of splits and does not specify that all will be split.
#a = ' qwe123dsa43** ***2*342rew '
#print re.split (' [\d*]+ ', a)
FindAll (string[, pos[, Endpos]) | Re.findall (pattern, string[, flags]):
Searches for a string, returning all matching substrings as a list.
p = re.compile (R ' \d+ ')
Print P.findall (' One1two2three3four4 ')
Sub (repl, string[, Count]) | Re.sub (Pattern, REPL, string[, Count]):
Returns the replaced string after each matched substring in string is replaced with REPL.
Count is used to specify the maximum number of replacements, not all when specified.
A= ' 321.423.432.432 33.43.5.42 1.2.443.34 255.52.53.255 2.2.2.2 3.3.3.3 '
Pattern=re.compile (R ' ([12]?\d{1,2}\.) {3} ([12]?\d{1,2})
Print pattern.sub (' Bibi ', a)
Finditer (string[, pos[, Endpos]) | Re.finditer (pattern, string[, flags]):
Searches for a string that returns an iterator that accesses each matching result (match object) sequentially.
A= ' 321.423.432.432 33.43.5.42 1.2.443.34 255.52.53.255 2.2.2.2 3.3.3.3 '
Pattern=re.compile (R ' ([12]?\d{1,2}\.) {3} ([12]?\d{1,2})
For I in Pattern.finditer (a):
Print (I.group ())
Python common modules