关于python:python-sys-模块

7次阅读

共计 2665 个字符,预计需要花费 7 分钟才能阅读完成。

python sys 模块

sys 模块次要提供了和 python 解释器进行交互的变量和函数;

import sys
#1. sys.argv[i], 获取命令行中传入的参数,第一个为模块 / 程序自身的名称, 从第二个元素开始才是真正的参数。#常常用于在服务器或终端运行 python 脚本时传大量的定制化或配置参数;#在以后门路下创立 main.py 模块,内容如下:import sys
a=sys.argv[1]
b=sys.argv[2]
print(sys.argv[0])
print('a is %s,b is %s'%(a,b))

#在内部通过命令行运行该文件
>>>(blog) D:\pycharmprojects>python main.py 'hh' 'test'
main.py#sys.argv[0]
a is 'hh',b is 'test'
#2. sys.version; 获取 python 版本名称
>>>sys.version
'3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)]'

#3. sys.path    #返回模块的搜寻门路
>>>sys.path
['D:\\pycharm\\PyCharm Community Edition 2021.1.1\\plugins\\python-ce\\helpers\\pydev', 'D:\\pycharmprojects', 'D:\\pycharm\\PyCharm Community Edition 2021.1.1\\plugins\\python-ce\\helpers\\third_party\\thriftpy', 'D:\\pycharm\\PyCharm Community Edition 2021.1.1\\plugins\\python-ce\\helpers\\pydev', 'C:\\Users\\Administrator\\anaconda\\envs\\blog\\python38.zip', 'C:\\Users\\Administrator\\anaconda\\envs\\blog\\DLLs', 'C:\\Users\\Administrator\\anaconda\\envs\\blog\\lib', 
 'C:\\Users\\Administrator\\anaconda\\envs\\blog', 'C:\\Users\\Administrator\\anaconda\\envs\\blog\\lib\\site-packages', #第三方模块的门路
 'D:\\pycharmprojects',
 'D:/pycharmprojects'# 以后门路 ]

 
 # 4. sys.platform    #返回操作系统平台名称
>>>sys.platform
'win32'
 
 # 5. sys.thread_info #以后线程信息
>>>sys.thread_info
sys.thread_info(name='nt', lock=None, version=None)
 
 # 6. sys.modules #以字典的模式返回所有以后 Python 环境中曾经导入的模块;留神是以后曾经加载或导入的;>>> sys.modules
{'sys': <module 'sys' (built-in)>, 'builtins': <module 'builtins' (built-in)>, '_frozen_importlib': <module 'importlib._bootstrap' (frozen)>, '_imp': <module '_imp' (built-in)>, '_warnings': <module …………}
 
 # 7. sys.builtin_module_names    #返回一个列表,蕴含所有曾经编译到 Python 解释器里的模块的名字
>>>sys.builtin_module_names
('_abc', '_ast', '_bisect', '_blake2', '_codecs', '_codecs_cn', '_codecs_hk', '_codecs_iso2022', '_codecs_jp', '_codecs_kr', '_codecs_tw', '_collections', '_contextvars', '_csv', '_datetime', '_functools', '_heapq', '_imp', '_io', '_json', '_locale', '_lsprof', '_md5', '_multibytecodec', '_opcode', '_operator', '_pickle', '_random', '_sha1', '_sha256', '_sha3', '_sha512', '_signal', '_sre', '_stat', '_statistics', '_string', '_struct', '_symtable', '_thread', '_tracemalloc', '_warnings', '_weakref', '_winapi', '_xxsubinterpreters', 'array', 'atexit', 'audioop', 'binascii', 'builtins', 'cmath', 'errno', 'faulthandler', 'gc', 'itertools', 'marshal', 'math', 'mmap', 'msvcrt', 'nt', 'parser', 'sys', 'time', 'winreg', 'xxsubtype', 'zlib')

 # 8.sys.stdin  用于命令行的交互式输出
 #read():将文件一次全副读取成一个字符串,包含特殊字符,须要较大内存
#readline():将文件依据换行符一行一行读取
#readlines():将文件一次性读取到内存,依行为单位读取为一个列表对象

res=0
while True:
a=int(sys.stdin.read())
res+=a
 
>1
>2
>3
>4
>Traceback (most recent call last):
  File "<input>", line 2, in <module>
ValueError: invalid literal for int() with base 10: '\n'
 
>>>res
10
 
 #9. sys.stdout 规范输入,print() 就是调用了该办法
>>> sys.stdout.write('this is a test\n')
this is a test
15# 还会打印出字符的数量 
正文完
 0