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

Case Analysis of Python Standard Library sys

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the Python standard library sys case analysis related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that everyone after reading this Python standard library sys case analysis article will have a harvest, let's take a look.

Sys action

Python's sys module provides an interface to access variables used or maintained by the interpreter and provides functions to interact with the interpreter and manipulate the runtime environment of Python.

> import sys common variable sys.version

Returns the version number of the Python interpreter; for a program, you need to run with the specified version number

> import sys > sys.version3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] sys.maxsize

Represents the maximum int value carried by the operating system

> > import sys > sys.maxsize9223372036854775807sys.maxunicode

An integer giving the maximum Unicode code point value, that is, 1114111 (hexadecimal 0x10FFFF).

> > import sys > sys.maxunicode1114111sys.path

The path environment variable of the current script. If there is no python, find it.

> import sys > sys.path [','D:\\ Python3.8\\ python38.zip', 'D:\\ Python3.8\\ DLLs', 'D:\\ Python3.8\\ lib', 'D:\\ Python3.8', 'D:\\ Python3.8\\ lib\\ site-packages'] sys.platform

Returns the name of the operating system platform, which is useful when writing cross-platform applications

The system returned value Windows'win32'Linux'linux'Mac'darwin' > import sys > sys.platformwin32sys.argv

Take the script name and parameters of the run time of the python script as a list and output. Realize the transfer of parameters from the outside of the program like the content of the program

Import sysprint (sys.argv) E:\ Python > python 2.py hello python ['2.pyrogen,' hello', 'python'] sys.executable

A string that gives the absolute path to the executable binaries of the Python interpreter. If Python cannot retrieve the real path to its executable, sys.executable will be an empty string or None.

> import sys > sys.executable'D:\\ Python3.8\\ python.exe'sys.byteorder

Indicator of the local byte order-the value is' big' on the large end order (most significant bit first) operating system and 'little on the small end order (least significant bit first) operating system

> > import sys > sys.byteorder'little'sys.version_info

Tuples of the five components that contain the version number: major,minor, micro,releaselevel, and serial.

> import sys > sys.version_infosys.version_info (major=3, minor=7, micro=3, releaselevel='final', serial=0) sys.api_version

The C API version of this interpreter.

> > import sys > sys.api_version1013sys.stdin/sys.stdout/sys.stderr

Standard input, output, error.

> import sys > sys.stdin > sys.stdout > sys.stderr Common method sys.exit ()

Exit the program, exit (0) when exiting normally

Import sysprint (sys.exit (0)) print (sys.exit (1)) sys.modules

Returns the module field imported by the system. Key is the module name and value is the module.

> > import sys > > sys.modules {'sys':,' builtins':,'_ frozen_importlib':,'_ imp':,'_ warnings':,'_ frozen_importlib_external':,'_ io':, 'marshal':,' nt':,'_ thread':,'_ weakref':, 'winreg':,' time':, 'zipimport':,' _ codecs':, 'codecs': 'encodings.aliases':, 'encodings':,' encodings.utf_8':,'_ codecs_cn':,'_ multibytecodec':, 'encodings.gbk':,' _ signal':,'_ _ main__':, 'encodings.latin_1':,' _ abc':, 'abc':,' io':,'_ stat':, 'stat':,' _ collections_abc': 'genericpath':, 'ntpath':,' os.path':, 'os':,' _ sitebuiltins':,'_ locale':,'_ bootlocale':, 'types':,' importlib._bootstrap':, 'importlib._bootstrap_external':,' warnings':, 'importlib':,' importlib.machinery':, 'importlib.abc':,' _ operator':, 'operator': 'keyword':,'_ heapq':, 'heapq':,' itertools':, 'reprlib':,' _ collections':, 'collections':,' _ functools':, 'functools':,' contextlib':, 'importlib.util':,' mpl_toolkits':, 'site':,' atexit':} sys.modules.keys ()

Returns a list of all imported module names

> import sys > sys.modules.keys () dict_keys (['sys',' builtins','_ frozen_importlib','_ imp','_ warnings','_ frozen_importlib_external','_ io', 'marshal',' nt','_ thread','_ weakref', 'winreg',' time', 'zipimport',' _ codecs', 'codecs',' encodings.aliases', 'encodings',' encodings.utf_8' '_ codecs_cn',' _ multibytecodec', 'encodings.gbk',' _ signal','_ _ main__', 'encodings.latin_1',' _ abc', 'abc',' io','_ stat', 'stat',' _ collections_abc', 'genericpath',' ntpath', 'os.path',' os','_ sitebuiltins','_ locale','_ bootlocale', 'types' 'importlib._bootstrap', 'importlib._bootstrap_external',' warnings', 'importlib',' importlib.machinery', 'importlib.abc',' _ operator', 'operator',' keyword','_ heapq', 'heapq',' itertools', 'reprlib',' _ collections', 'collections',' _ functools', 'functools',' contextlib', 'importlib.util',' mpl_toolkits', 'site' 'atexit']) sys.getdefaultencoding ()

Returns the name of the current default string encoding used by the Unicode implementation.

> import sys > sys.getdefaultencoding () 'utf-8'sys.getfilesystemencoding ()

Returns the encoded name used to convert between Unicode file names and byte file names

> import sys > sys.getfilesystemencoding () utf-8sys.getrecursionlimit ()

Returns the maximum number of recursions

> import sys > sys.getrecursionlimit () # View the maximum recursion depth of the current interpreter 1000sys.setrecursionlimit (num)

Set the maximum number of recursions

> import sys > sys.setrecursionlimit (1100) # set the maximum recursion depth of the interpreter to 1100 > sys.getrecursionlimit () # again view the maximum recursion depth of the current interpreter 1100sys.getsizeof ()

Gets the amount of memory occupied by the object (in bytes)

> import sys > > for obj in [int (), float (), list (), tuple (), set (), dict (), object]:. Print (str (obj.__class__) .ljust (20), sys.getsizeof (obj)). 24 24 56 40 216 232 416sys.getrefcount (obj)

Returns the reference count of obj. The returned count is usually one higher than expected because it contains a (temporary) reference as a parameter.

> import sys > a = [1Jing 2Jue 3] > b = a > > c = b > sys.getrefcount (a) 4sys.exc_info ()

Get the details of the exception classes currently being handled, such as exc_type, exc_value, and exc_traceback.

> import sys > sys.exc_info () (None, None, None) sys.getwindowsversion ()

Get the version of Windows, which is valid in Windows system

> import sys > > sys.getwindowsversion () sys.getwindowsversion (major=10, minor=0, build=19041, platform=2, service_pack='') sys.stdin.readline ()

Reading a line from standard input will read the newline character at the end

Sys.stdout.write ()

Write to standard output

> import sys > I believe that everyone has a certain understanding of the knowledge of "Python standard library sys case analysis". If you want to learn more knowledge, you are welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report