python sys 模块

Posted 千翻娃儿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python sys 模块相关的知识,希望对你有一定的参考价值。

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#还会打印出字符的数量

以上是关于python sys 模块的主要内容,如果未能解决你的问题,请参考以下文章

python内置模块(sys)--033

Python中常用的模块(sys模块)

Python中sys模块

python标准库之sys模块 学习

python 常用模块之random,os,sys 模块

python sys.path 从哪里