How to use the sys system module and logging log module of python
This article mainly introduces the "python sys system module and logging log module how to use" related knowledge, editor through the actual case to show you the operation process, the operation method is simple and fast, practical, I hope that this "python sys system module and logging log module how to use" the article can help you solve the problem.
Sys module
Enter:
#! / usr/bin/python
# Filename: versioncheck.py
Import sys, warnings
Print (sys.version_info)
If sys.version_info [0] < 3:
Warnings.warn ("Need Python 3.0 for this program to run", RuntimeWarning)
Else:
Print ('Proceed as normal')
Output:
$python2.5 versioncheck.py
(3,0,0, 'beta', 2)
Versioncheck.py:6: RuntimeWarning: Need Python 3.0 for this program to run RuntimeWarning) $python3 versioncheck.py Proceed as normal
Explanation:
System module, you can get the version information of the system, we can use if statements to judge, version information.
Sys.version_info outputs version information
Logging module
Enter:
#! / usr/bin/python
# Filename: use_logging.py
Import os, platform, logging
If platform.platform () .startswith ('Windows'):
Logging_file = os.path.join (os.getenv ('HOMEDRIVE'), os.getenv (' HOMEPATH'), 'test.log')
Else:
Logging_file = os.path.join (os.getenv ('HOME'),' test.log')
Logging.basicConfig (level=logging.DEBUG, format='% (asctime) s:% (levelname) s:% (message) slots, filename = logging_file, filemode = 'wicked,)
Logging.debug ("Start of the program") logging.info ("Doing something") logging.warning ("Dying now")
Output:
$python use_logging.py
Logging to C:\ Users\ swaroop\ test.log
The contents of the test.log file:
2018-09-03 13 purl 18 purl 16 233: DEBUG: Start of the program
2018-09-03 13 purl 18 purl 16 233: INFO: Doing something
2018-09-03 13 purl 18 purl 16 233: WARNING: Dying now
Explanation:
On the one hand, through the string to judge whether the platform information obtained by the platform module is a Windows system.
On the other hand, by using the os module, the time information is obtained, and when recorded in the log module, the time information is added.
This is the end of the content about "how to use the sys system module and logging log module of python". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.