python 解释器交互模块 -- sys
sys模块是与python解释器交互的一个接口
sys.argv 命令行参数List,第一个元素是程序本身路径
sys.getdefaultencoding(): 获取系统当前编码,一般默认为ascii。
sys.setdefaultencoding(): 设置系统默认编码,执行dir(sys)时不会看到这个方法,在解释器中执行不通过,可以先执行reload(sys),在执行 setdefaultencoding('utf8'),此时将系统默认编码设置为utf8。(见设置系统默认编码 )。
sys.getfilesystemencoding(): 获取文件系统使用编码方式,Windows下返回'mbcs',mac下返回'utf-8'.
sys.modules.keys() 返回所有已经导入的模块列表
sys.exc_info() 获取当前正在处理的异常类,exc_type、exc_value、exc_traceback当前处理的异常详细信息
sys.exit(n) 退出程序,正常退出时exit(0)
sys.hexversion 获取Python解释程序的版本值,16进制格式如:0x020403F0
sys.version 获取Python解释程序的版本信息
sys.maxint 最大的Int值
sys.maxunicode 最大的Unicode值
sys.modules 返回系统导入的模块字段,key是模块名,value是模块
sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
sys.platform 返回操作系统平台名称
sys.stdout 标准输出
sys.stdin 标准输入
sys.stderr 错误输出
sys.exc_clear() 用来清除当前线程所出现的当前的或最近的错误信息
sys.exec_prefix 返回平台独立的python文件安装的位置
sys.byteorder 本地字节规则的指示器,big-endian平台的值是'big',little-endian平台的值是'little'
sys.copyright 记录python版权相关的东西
sys.api_version 解释器的C的API版本
1,argv : 处理命令行参数
在解释器启动后, argv 列表包含了传递给脚本的所有参数, 列表的第一个元素为脚本自身的名称.
import sys
print(sys.argv[0]) 结果:D:/pythonfile/PyCharm/week6/da.py
2,path : 处理模块
path 列表是一个由目录名构成的列表, Python 从中查找扩展模块( Python 源模块, 编译模块,或者二进制扩展).
启动 Python 时,这个列表从根据内建规则, PYTHONPATH 环境变量的内容, 以及注册表( Windows 系统)等进行初始化.
由于它只是一个普通的列表, 你可以在程序中对它进行操作,
import sys print(sys.path) 结果 : ['D:\\pythonfile\\PyCharm\\week6', 'D:\\pythonfile\\PyCharm', 'D:\\pythonfile\\PyCharm\\venv\\Scripts\\python37.zip', 'C:\\python37\\Lib', 'C:\\python37\\DLLs', "C:\\Users\\deng'peng\\AppData\\Local\\Programs\\Python\\Python37-32", 'D:\\pythonfile\\PyCharm\\venv', 'D:\\pythonfile\\PyCharm\\venv\\lib\\site-packages', 'D:\\pythonfile\\PyCharm\\venv\\lib\\site-packages\\setuptools-39.1.0-py3.7.egg', 'D:\\pythonfile\\PyCharm\\venv\\lib\\site-packages\\pip-10.0.1-py3.7.egg', 'D:\\软件\\PyCharm 2018.1.4\\helpers\\pycharm_matplotlib_backend']
3,modules : 查找已导入的模块
全局字典,每当python启动时该字段自动加载到内存中。新加模块sys.modules会自动记录该模块,第二次导入时直接从字典中加载,加快运行速度。他拥有字典的一切方法。
keys是模块名
values是模块
modules返回路径
import sys print(sys.modules)
print("``````````````")
print(sys.modules.keys())
print("``````````````")
print(sys.modules.values())
print("```````````````````")
print(sys.modules["os"] 运行结果:
{'sys': <module 'sys' (built-in)>, 'builtins': <module 'builtins' (built-in)>, '_frozen_importlib': <module '_frozen_importlib' (frozen)>, '_imp': <module '_imp' (built-in)>, '_thread': <module '_thread' (built-in)>, '_warnings': <module '_warnings' (built-in)>, '_weakref': <module '_weakref' (built-in)>, 'zipimport': <module 'zipimport' (built-in)>, '_frozen_importlib_external': <module '_frozen_importlib_external' (frozen)>, '_io': <module 'io' (built-in)>, 'marshal': <module 'marshal' (built-in)>, 'nt': <module 'nt' (built-in)>, 'winreg': <module 'winreg' (built-in)>, 'encodings': <module 'encodings' from 'C:\\python37\\Lib\\encodings\\__init__.py'>, 'codecs': <module 'codecs' from 'C:\\python37\\Lib\\codecs.py'>, '_codecs': <module '_codecs' (built-in)>, 'encodings.aliases': <module 'encodings.aliases' from 'C:\\python37\\Lib\\encodings\\aliases.py'>, 'encodings.utf_8': <module 'encodings.utf_8' from 'C:\\python37\\Lib\\encodings\\utf_8.py'>, '_signal': <module '_signal' (built-in)>, '__main__': <module '__main__' from 'D:/pythonfile/PyCharm/week6/day34.py'>, 'encodings.latin_1': <module 'encodings.latin_1' from 'C:\\python37\\Lib\\encodings\\latin_1.py'>, 'io': <module 'io' from 'C:\\python37\\Lib\\io.py'>, 'abc': <module 'abc' from 'C:\\python37\\Lib\\abc.py'>, '_abc': <module '_abc' (built-in)>, 'site': <module 'site' from 'C:\\python37\\Lib\\site.py'>, 'os': <module 'os' from 'C:\\python37\\Lib\\os.py'>, 'stat': <module 'stat' from 'C:\\python37\\Lib\\stat.py'>, '_stat': <module '_stat' (built-in)>, 'ntpath': <module 'ntpath' from 'C:\\python37\\Lib\\ntpath.py'>, 'genericpath': <module 'genericpath' from 'C:\\python37\\Lib\\genericpath.py'>, 'os.path': <module 'ntpath' from 'C:\\python37\\Lib\\ntpath.py'>, '_collections_abc': <module '_collections_abc' from 'C:\\python37\\Lib\\_collections_abc.py'>, '_sitebuiltins': <module '_sitebuiltins' from 'C:\\python37\\Lib\\_sitebuiltins.py'>, '_bootlocale': <module '_bootlocale' from 'C:\\python37\\Lib\\_bootlocale.py'>, '_locale': <module '_locale' (built-in)>, 'encodings.gbk': <module 'encodings.gbk' from 'C:\\python37\\Lib\\encodings\\gbk.py'>, '_codecs_cn': <module '_codecs_cn' (built-in)>, '_multibytecodec': <module '_multibytecodec' (built-in)>, 'encodings.cp437': <module 'encodings.cp437' from 'C:\\python37\\Lib\\encodings\\cp437.py'>, 'sitecustomize': <module 'sitecustomize' from 'D:\\软件\\PyCharm 2018.1.4\\helpers\\pycharm_matplotlib_backend\\sitecustomize.py'>, 'time': <module 'time' (built-in)>, 'random': <module 'random' from 'C:\\python37\\Lib\\random.py'>, 'warnings': <module 'warnings' from 'C:\\python37\\Lib\\warnings.py'>, 'types': <module 'types' from 'C:\\python37\\Lib\\types.py'>, 'math': <module 'math' (built-in)>, 'hashlib': <module 'hashlib' from 'C:\\python37\\Lib\\hashlib.py'>, '_hashlib': <module '_hashlib' from 'C:\\python37\\DLLs\\_hashlib.pyd'>, '_blake2': <module '_blake2' (built-in)>, '_sha3': <module '_sha3' (built-in)>, 'itertools': <module 'itertools' (built-in)>, 'bisect': <module 'bisect' from 'C:\\python37\\Lib\\bisect.py'>, '_bisect': <module '_bisect' (built-in)>, '_random': <module '_random' (built-in)>}
``````````````
dict_keys(['sys', 'builtins', '_frozen_importlib', '_imp', '_thread', '_warnings', '_weakref', 'zipimport', '_frozen_importlib_external', '_io', 'marshal', 'nt', 'winreg', 'encodings', 'codecs', '_codecs', 'encodings.aliases', 'encodings.utf_8', '_signal', '__main__', 'encodings.latin_1', 'io', 'abc', '_abc', 'site', 'os', 'stat', '_stat', 'ntpath', 'genericpath', 'os.path', '_collections_abc', '_sitebuiltins', '_bootlocale', '_locale', 'encodings.gbk', '_codecs_cn', '_multibytecodec', 'encodings.cp437', 'sitecustomize', 'time', 'random', 'warnings', 'types', 'math', 'hashlib', '_hashlib', '_blake2', '_sha3', 'itertools', 'bisect', '_bisect', '_random'])
``````````````
dict_values([<module 'sys' (built-in)>, <module 'builtins' (built-in)>, <module '_frozen_importlib' (frozen)>, <module '_imp' (built-in)>, <module '_thread' (built-in)>, <module '_warnings' (built-in)>, <module '_weakref' (built-in)>, <module 'zipimport' (built-in)>, <module '_frozen_importlib_external' (frozen)>, <module 'io' (built-in)>, <module 'marshal' (built-in)>, <module 'nt' (built-in)>, <module 'winreg' (built-in)>, <module 'encodings' from 'C:\\python37\\Lib\\encodings\\__init__.py'>, <module 'codecs' from 'C:\\python37\\Lib\\codecs.py'>, <module '_codecs' (built-in)>, <module 'encodings.aliases' from 'C:\\python37\\Lib\\encodings\\aliases.py'>, <module 'encodings.utf_8' from 'C:\\python37\\Lib\\encodings\\utf_8.py'>, <module '_signal' (built-in)>, <module '__main__' from 'D:/pythonfile/PyCharm/week6/day34.py'>, <module 'encodings.latin_1' from 'C:\\python37\\Lib\\encodings\\latin_1.py'>, <module 'io' from 'C:\\python37\\Lib\\io.py'>, <module 'abc' from 'C:\\python37\\Lib\\abc.py'>, <module '_abc' (built-in)>, <module 'site' from 'C:\\python37\\Lib\\site.py'>, <module 'os' from 'C:\\python37\\Lib\\os.py'>, <module 'stat' from 'C:\\python37\\Lib\\stat.py'>, <module '_stat' (built-in)>, <module 'ntpath' from 'C:\\python37\\Lib\\ntpath.py'>, <module 'genericpath' from 'C:\\python37\\Lib\\genericpath.py'>, <module 'ntpath' from 'C:\\python37\\Lib\\ntpath.py'>, <module '_collections_abc' from 'C:\\python37\\Lib\\_collections_abc.py'>, <module '_sitebuiltins' from 'C:\\python37\\Lib\\_sitebuiltins.py'>, <module '_bootlocale' from 'C:\\python37\\Lib\\_bootlocale.py'>, <module '_locale' (built-in)>, <module 'encodings.gbk' from 'C:\\python37\\Lib\\encodings\\gbk.py'>, <module '_codecs_cn' (built-in)>, <module '_multibytecodec' (built-in)>, <module 'encodings.cp437' from 'C:\\python37\\Lib\\encodings\\cp437.py'>, <module 'sitecustomize' from 'D:\\软件\\PyCharm 2018.1.4\\helpers\\pycharm_matplotlib_backend\\sitecustomize.py'>, <module 'time' (built-in)>, <module 'random' from 'C:\\python37\\Lib\\random.py'>, <module 'warnings' from 'C:\\python37\\Lib\\warnings.py'>, <module 'types' from 'C:\\python37\\Lib\\types.py'>, <module 'math' (built-in)>, <module 'hashlib' from 'C:\\python37\\Lib\\hashlib.py'>, <module '_hashlib' from 'C:\\python37\\DLLs\\_hashlib.pyd'>, <module '_blake2' (built-in)>, <module '_sha3' (built-in)>, <module 'itertools' (built-in)>, <module 'bisect' from 'C:\\python37\\Lib\\bisect.py'>, <module '_bisect' (built-in)>, <module '_random' (built-in)>])
```````````````````
<module 'os' from 'C:\\python37\\Lib\\os.py'>
4,platform : 获得当前平台
sys.platform 返回当前平台 出现如: "win32" "linux2" 等
print(sys.platform) 结果:win32
5,exit : 退出程序
如果准备在退出前自己清理一些东西(比如删除临时文件), 你可以配置一个 "退出处理函数"(exit handler), 它将在程序退出的时候自动被调用
import sys
print("hello")
try:
sys.exit(1)
except SystemExit: # 捕获退出的异常
pass # 捕获后不做任何操作
print("There") 结果:
hello
There
python 解释器交互模块 -- sys的更多相关文章
- python--模块之sys与python解释器交互模块
作用:sys模块是与python解释器交互的一个接口.它提供了一系列有关python运行环境的变量和函数. 常用函数:import sys sys.argv #命令行参数list,第一个元素是程序本身 ...
- Python内置的操作系统模块(os)与解释器交互模块(sys)
Python内置的操作系统模块(os)与解释器交互模块(sys) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本片博客只介绍Python调用操作系统的模块即os模块,以及Pyth ...
- Python基础-os模块 sys模块
sys模块 与操作系统交互的一个接口 文件夹相关 os.makedirs('dirname1/dirname2') 可生成多层递归目录 os.removedirs('dirname1') ...
- sys模块-与python解释器交互的模块
需要 import sys a=sys.platform #获取当前系统平台 #如果是window系统就返回‘win32’#如果是linux系统就返回‘linux’#如果是Windows/Cyg ...
- os模块 sys模块 json/pickle 模块 logging模块
目录 模块 1. os模块 2. sys模块 3. json和pickle模块 4. logging模块 (1)日志配置 (2)实际开发中日志的使用 模块 1. os模块 os模块有什么用 与操作系统 ...
- python 全栈开发,Day27(复习, defaultdict,Counter,时间模块,random模块,sys模块)
一.复习 看下面一段代码,假如运行结果有问题,那么就需要在每一步计算时,打印一下结果 b = 1 c = 2 d = 3 a = b+c print(a) e = a + d print(e) 执行输 ...
- python常用模块: random模块, time模块, sys模块, os模块, 序列化模块
一. random模块 import random # 任意小数 print(random.random()) # 0到1的任意小数 print(random.uniform(-10, 10)) # ...
- Python基础-os和sys模块
os模块提供对操作系统进行调用的接口 import os os.getcwd() # 获取当前工作目录 os.chdir(r'd:\fansik') # 修改对当前工作目录 print(os.curd ...
- Python之常用模块(re,时间,random,os,sys,序列化模块)(Day20)
一.时间模块 #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.time() 获取当前时间戳 在Python中表示时间的三种方式:时间戳,元组(s ...
随机推荐
- 高速LVDS电平简介
一.LVDS简介 1.1.LVDS信号介绍LVDS:Low Voltage Differential Signaling,低电压差分信号.LVDS传输支持速率一般在155Mbps(大约为77MHZ)以 ...
- ASP.NET Core 使用外部登陆提供程序登陆的流程,以及身份认证的流程 (转载)
阅读目录 在Asp.Net Core 中使用外部登陆(google.微博...) 中间件管道 The Authentication Middleware The Challenge 与认证中间件进行交 ...
- Vue-详解设置路由导航的两种方法: <router-link :to="..."> 和router.push(...)
一.<router-link :to="..."> to里的值可以是一个字符串路径,或者一个描述地址的对象.例如: // 字符串 <router-link to= ...
- Ionic Android项目Splash设置
ionic项目中,在splashscreen消失后会出现零点几秒的白屏,再出现app页面. 1. 安装Cordova splash screen插件 ionic plugin add org.apac ...
- EF性能优化-有人说EF性能低,我想说:EF确实不如ADO.NET
十年河东,十年河西,莫欺少年穷. EF就如同那个少年,ADO.NET则是一位壮年.毕竟ADO.NET出生在EF之前,而EF所走的路属于应用ADO.NET. 也就是说:你所写的LINQ查询,最后还是要转 ...
- 求组合数、求逆元、求阶乘 O(n)
在O(n)的时间内求组合数.求逆元.求阶乘.·.· #include <iostream> #include <cstdio> #define ll long long ;// ...
- ABP从入门到精通(5):.扩展国际化语言资源
ABP的有些组件使用的该组件自带的语言包资源,所以在有些时候会因为我们当前使用的语言对应的语言包不全,而造成日志一直记录WARN.ABP给我们提供了扩展语言包资源的接口,可以解决这个问题. 以下示例代 ...
- 分布式全文搜索引擎ElasticSearch
一 什么是 ElasticSearch Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎.当然 Elas ...
- 数组排列组合问题——BACKTRACKING
BACKTRACKING backtracking(回溯法)是一类递归算法,通常用于解决某类问题:要求找出答案空间中符合某种特定要求的答案,比如eight queens puzzle(将国际象棋的八个 ...
- BZOJ3782 上学路线
设障碍个数为,\(obs\)则一般的容斥复杂度为\(O(2^{obs})\).但因为这个题是网格图,我们可以用DP解.设\(f[i]\)表示不经过任何障碍到达第\(i\)个障碍的方案数,转移时枚举可以 ...