Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the sys system module and logging log module of python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report