Python - sys 模块

作用:让你能够访问与Python解释器紧密相关的变量和函数

sys.argv

"""
sys.argv: 获得python 文件执行时的命令行参数,包括脚本名,返回的是一个list
注意:
1、使用时一般要做一个副本,在副本上操作
"""
import sys

# 命令行执行: PS E:\PyProject\study\test> python reverse_args.py this is a test

print(sys.argv)  # out: ['reverse_args.py', 'this', 'is', 'a', 'test']

args = sys.argv[1:] 
print(args) # out: ['this', 'is', 'a', 'test']

sys.getdefaultencoding()

 """ Return the current default encoding used by the Unicode implementation. """
>>> sys.getdefaultencoding()
'utf-8'

python版本信息查看

>>> sys.version_info
sys.version_info(major=3, minor=10, micro=1, releaselevel='final', serial=0)
>>> sys.version
'3.10.1 (tags/v3.10.1:2cd268a, Dec  6 2021, 19:10:37) [MSC v.1929 64 bit (AMD64)]'

sys.path

# import 语句执行时,python 搜索模块文件的路径
>>> sys.path
['', 'E:\\code_tool\\python\\python310.zip', 'E:\\code_tool\\python\\DLLs', 'E:\\code_tool\\python\\lib', 'E:\\code_tool\\python', 'E:\\code_tool\\python\\lib\\site-packages']

sys.platform

# 运行python 解释器的平台
>>> sys.platform
'win32'

sys.getrefcount

>>> class A:
...     pass
...
>>>
>>> a = A()
>>> aa = A()
>>>
>>> sys.getrefcount(a)
2
>>> sys.getrefcount(aa)
2
>>>
>>> del aa
>>> sys.getrefcount(aa)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'aa' is not defined. Did you mean: 'a'?
posted @ 2022-01-02 20:10  chuangzhou  阅读(30)  评论(0编辑  收藏  举报