OS Module features: Provides an interface to 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 () 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 "\ r \ n", Linux "\ n"
os.pathsep output string for splitting the file path
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.environ getting system environment variables
* os.path.abspath (path) returns the absolute path normalized by path
os.path.split (path) splits path into directory and file name two tuples returned
* os.path.dirname (path) returns the directory of path. is actually the first element of Os.path.split (path)
os.path.basename (path) 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, 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[, ...]) 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
sys module: Interacting with the Python interpreter
sys.argv command line argument list, the first element is the program itself path
Sys.exit (n) exit program, Exit normally (0)
sys.version get P Ython version information for the interpreter
Sys.maxint the largest int value
Sys.path returns the search path to the module, initialized with the value of the PYTHONPATH environment variable
Sys.platform returns the operating system platform name
Sys.stdout.write (' P Lease: ')
val = Sys.stdin.readline () [: -1]
1 Import OS 2 Import SYS 3 4 base_dir = Os.path.dirname (Os.path.dirname (Os.path.dirname (__file__)))5 Print (Base_dir) 6 sys.path.append (Base_dir)
Hashlib module: Encryption (MD5,SHA,SSL)
transferred from: https://www.cnblogs.com/yuanchenqi/articles/5732581.html
Logging Module
The default logging level is set to WARNING (log level level critical > ERROR > WARNING > INFO > DEBUG > NOTSET),
The default log format is log level: Logger name: User output message.
the Logging.basicconfig () function can be seen to change the default behavior of the logging module through specific parameters, with the parameters available
FileName: Creates a filedhandler with the specified file name (the concept of handler is explained in the back) 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 format 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 thread ID. Probably not .
% (threadname) s thread name. Probably not .
% (process) d process ID. Probably not .
% (message) s user-output message
configparser module: Used to generate and modify common configuration documents, Python3 module name changed to Configparser
1 #used to build and modify common configuration documents2 ImportConfigparser3 4Config =Configparser. Configparser ()5 6 " "7 #写入的第一种方法8 config["DEFAULT"] = {' Serveraliveinterval ': ' $ ',9 ' Compression ': ' Yes ',Ten ' compressionlevel ': ' 9 '} One A config[' bitbucket.org ' = {' User ': ' HG '} - - #第二种方法 the config[' topsecret.server.com ' = {} - Topsecret = config[' topsecret.server.com '] - topsecret[' Host Port ' = ' 50022 ' # mutates the parser - topsecret[' ForwardX11 ' = ' no ' # same here + - #第三种方法 + config[' DEFAULT ' [' ForwardX11 '] = ' yes ' A at #写入 - with open (' Example.ini ', ' W ') as ConfigFile: - config.write (configfile) - " " - - #Read Content inConfig.read ('Example.ini') - #print (Config.sections ()) to #print (Config.defaults ()) + # - #print (config[' DEFAULT ' [' ForwardX11 ']) the * #For key in config: $ #print (key)Panax Notoginseng - #For key in config[' bitbucket.org ']: the #print (key) + A the #Delete +Config.remove_section ('topsecret.server.com') - $ ##有没有这个字符串 $ #C = config.has_section (' topsecret.server.com ') - #print (c) - # the ##修改 - #config.set (' bitbucket.org ', ' user ', ' Alex ')Wuyi # the # - ##删除 Wu #config.remove_option (' bitbucket.org ', ' user ') - AboutConfig.write (Open ('C','W'))
Re module:
Regular Expressions: matching strings
the method provided by the string is an exact match, so the regular expression is used
through the RE module implementation
character match (normal character, metacharacters)
Meta-character: ^. $ * + ? {} [] | or () \
. Wildcards, which can only be used to refer to any one character, except for line breaks \ n
^ Only at the beginning of the match
$ only at the end of the match
* Repeat match [0,+]
+ repeat match [1,+]
? Match [0,1]
{} matches any number, it can add range
[] Character set: or the relationship can also cancel special functions of metacharacters (\ ^-) Exceptions
^ put in [] inside, take counter
\ 1> Backslash followed by meta-character removal of special functions, such as \.
2> backslash followed by ordinary characters to achieve special functions, such as \d
() Grouping
a backslash is followed by a meta-character to remove special functions, such as \.
a backslash followed by a normal character to implement special functions, such as \d
\d matches any decimal number; it is equivalent to class [0-9].
\d matches any non-numeric character; it is equivalent to class [^0-9].
\s matches any whitespace character; it is equivalent to class [\t\n\r\f\v].
\s matches any non-whitespace character; it is equivalent to class [^ \t\n\r\f\v].
\w matches any alphanumeric character; it is equivalent to class [a-za-z0-9_].
\w matches any non-alphanumeric character; it is equivalent to a class [^a-za-z0-9_]
\b Matches a special character boundary, such as a space, &,#, etc.
methods of regular Expressions:
FindAll () All the results are returned to a list
Search () returns a match to the first object (object), which can be fished by the group () method to fetch the returned result
Match () only begins to match the string and returns only one object. Object calls the group method to fetch the result
* split () split, multiple split , first split first, and then split on the basis of a second element
Sub () substitution, similar to replace
subn () is just like a sub, but returns a replacement several times.
compile () defines a rule to be called later
Finditer () encapsulated as an iterator
< note:
In the creation of a file, there was an error, accidentally naming the file name in order to block the name of the module, resulting in errors I hope you notice!
Python-os Module