Python Four week five lessons

Source: Internet
Author: User

One, logging module

Logging modules are typically used to print log information, log level critical > Error > Warning > Info > Debug > NotSet, lower log level output more information

Logging usage 1: Print log information directly to the screen and print only log information above warning level by default

 #!/usr/bin/python Import Logginglogging.debug (   this is debug message    ) Logging.info (  "  This is info message   " ) logging.warning (   " this is warning message   '   ' Logging.error (  " this is error message   ) 
1is an error message 

Logging Usage 2: Save log information to a file by using the Basicconfig () function

#!/usr/bin/pythonimport logginglogging.basicconfig ( level=logging. DEBUG, Format='% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s', # Set the output format of the log information datefmt='%a,%d%b%Y%h:%m:%s', # Set the time format for log information filename='Myapp.log', # Set log information to which file to save FileMode='W') # Specifies the open mode of the log file Logging.debug ('This is debug message') Logging.info ('This is info message') logging.warning ('This is warning message')
[[email protected] ~]# python1. py [[email protected]~]# cat Myapp.log Fri,TenNov .  +: A: One 1. Py[line:Ten] DEBUG This isDebug Messagefri,TenNov .  +: A: One 1. Py[line: One] INFO This isInfo Messagefri,TenNov .  +: A: One 1. Py[line: A] WARNING This isWarning message

Output format:

% (Levelno) S: Print the value of the log level
% (levelname) S: Print log level name
% (pathname) s: Prints the path of the currently executing program, which is actually sys.argv[0]
% (filename) s: Prints the current name of the executing program
% (funcName) s: Print the current function of the log
% (Lineno) d: Print the current line number of the log
% (asctime) s: Time to print logs
% (thread) d: Print thread ID
% (threadname) s: Print thread name
% (process) d: Print process ID
% (message) s: Print log information

Second, OS module

Os.listdir (): Used to print a list of files in a specified directory, such as Os.listdir ('/tmp/')

Os.path.isdir (): Used to determine if the directory exists, if present returns True if there is no return False

Os.path.isfile (): Used to determine if the file exists, if present returns True if there is no return False

Os.path.join (): Used to connect two parameters, usually used to connect the file name, such as Os.path.join (' etc ', ' passwd ') returns the result as '/etc/passwd '

Os.path.split (): Used to separate directories and files, such as Os.path.split ('/etc/passwd ') returns the result as ('/etc ', ' passwd ')

Os.path.exists (): Used to determine if a variable, file, etc. exists, returns True if there is no return False if there is no

Os.path.getsize (): Used to get the size of the file, by default in K, such as Os.path.getsize ('/tmp/1.txt ')

Os.path.basename (): Returns only the file name part, such as Os.path.basename ('/etc/passwd ') returns the result as ' passwd '

Os.path.dirname (): Returns only the directory part, such as Os.path.basename ('/etc/passwd '), returns the result as '/etc '

OS.GETCWD (): Used to get the current directory

Os.chdir (): Used to change the working directory, equivalent to a CD command, such as Os.chdir ('/tmp ') equivalent to Cd/tmp

Os.mkdir (): Used to create the directory, equivalent to the mkdir command, such as Os.mkdir ('/tmp/test ') equivalent to Mkdir/tmp/test

Os.mkdirs (): Used to recursively create a directory, such as Os.mkdirs ('/tmp/a/b/c ') equivalent to Mkdir-p/tmp/a/b/c

Os.remove (): Used to delete the specified file, such as Os.remove ('/tmp/1.txt ')

Os.rmdir (): Used to delete the specified directory, such as Os.rmdir ('/tmp/test ')

Os.walk (): For files in the iteration directory, detailed usage

Os.name: Judging system platform, Windows system returns ' NT ', Linux system returns ' POSIX '

Os.system (): Used to execute shell commands, such as Os.system (' ls/tmp '), detailed usage

Os.popen (): Used to execute shell commands, but only return standard output, detailed usage

Os.popen2 (): Used to execute shell commands, return standard input and standard output, detailed usage

Os.popen3 (): Used to execute shell commands, return standard input, standard output, standard error output, detailed usage

Three, commands module

Commands modules are typically used to execute some shell commands

Commands.getoutput (): Used to execute shell commands, such as Commands.getoutput (' ls/tmp/')

Commands.getstatusoutput (): Used to execute shell commands and return status codes after command execution

Four, sys module

Sys.exit (): Used to exit the entire script

Sys.stdin.read (): For receiving standard input, waiting for keyboard input after execution

Sys.stdout.write (): Used to output standard output, such as Sys.stdout.write (' Hello ') will print out Hello, default non-newline character

Sys.stderr.write (): Used to output standard error output, such as Sys.stderr.write (' Hello ') will print out Hello, default non-newline character

SYS.ARGV: Used to receive command-line arguments, consistent with $n usage in shell scripts, such as Sys.argv[0 ] equivalent to $ (that is, file name), Sys.argv[1] equivalent to $ (That is, the first parameter), Sys.argv[2 ] is equivalent to $ (the second argument), and so on

Python Four week five lessons

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.