OS Module
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
Judging the target
Os.path.exists ("goal") to determine if the target exists
Os.path.isdir ("goal") determine whether the target directory
Os.path.isfile ("goal") determine if the target is a file
SYS module
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]
SYS module will be used to Sys.path
#-*-Coding:utf-8-*-import os,sys,logging# get the basic directory for this program Base_dir = Os.path.dirname (Os.path.dirname (Os.path.abspath (__ file__)) #添加到系统目录sys. Path.insert (1,base_dir) account = ' Channel ' #指定日志文件的存放目录 #account_log = R '%s\logs\%s '% (Base_dir, account) Account_log = R ' {}\logs\{} '. Format (base_dir,account) print (base_dir,account) print (account_log) if not Os.path.exists (Account_log): with open (Account_log, ' W ') as F: F.write (' This is a new log file.\n ') F.flushelse: with open (Account_log, ' a ') as F: f.write (' Write something by yourself.\n ') F.flush ()
Logging module
Logging log can be divided into,, debug()
info()
warning()
, and error()
critical() 5个级别,
below we look at how to use.
Import Logging Logging.basicconfig (filename= ' Example.log ', level=logging.info) logging.debug (' This message should go To the log file ') Logging.info (' so should this ') logging.warning (' And this, too ')
The level=loggin in this sentence. Info means that the logging level is set to info, that is, only logs that are higher than the log is info or the info level will be recorded in the file, in this case, the first log is not recorded, if you want to record the debug log, Change the log level to debug on the line.
Format of the Log
% (name) s |
Logger's name. |
% (Levelno) s |
Log level in digital form |
% (LevelName) s |
Log level in text form |
% (pathname) s |
The full path name of the module that called the log output function 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 |
Function name of the call log output function |
% (Lineno) d |
The line of code where the statement that called the log output function is located |
% (created) f |
Current time, represented by the UNIX standard floating-point number representing the time |
% (relativecreated) d |
The number of milliseconds since logger was created when the log information is output |
% (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 |
The thread ID. Probably not. |
% (ThreadName) s |
The name of the thread. Probably not. |
% (process) d |
The process ID. Probably not. |
% (message) s |
User-Output messages |
|
|
Import logginglogging.basicconfig (filename= ' Log.log ', format= '% (asctime) s-% (name) s-% (LevelName) s-% (module) s : % (message) s ', datefmt= '%y-%m-%d%h:%m:%s%p ', level=logging.info) logging.debug (' Debug ') Logging.info (' info ') logging.warning (' Warning ') logging.error (' Error ') logging.critical (' critical ') Logging.log (10 , ' log ')
Use of the Python os,sys,logging module