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的更多相关文章

  1. python--模块之sys与python解释器交互模块

    作用:sys模块是与python解释器交互的一个接口.它提供了一系列有关python运行环境的变量和函数. 常用函数:import sys sys.argv #命令行参数list,第一个元素是程序本身 ...

  2. Python内置的操作系统模块(os)与解释器交互模块(sys)

    Python内置的操作系统模块(os)与解释器交互模块(sys) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本片博客只介绍Python调用操作系统的模块即os模块,以及Pyth ...

  3. Python基础-os模块 sys模块

    sys模块 与操作系统交互的一个接口 文件夹相关 os.makedirs('dirname1/dirname2')    可生成多层递归目录 os.removedirs('dirname1')    ...

  4. sys模块-与python解释器交互的模块

    需要  import sys a=sys.platform   #获取当前系统平台 #如果是window系统就返回‘win32’#如果是linux系统就返回‘linux’#如果是Windows/Cyg ...

  5. os模块 sys模块 json/pickle 模块 logging模块

    目录 模块 1. os模块 2. sys模块 3. json和pickle模块 4. logging模块 (1)日志配置 (2)实际开发中日志的使用 模块 1. os模块 os模块有什么用 与操作系统 ...

  6. python 全栈开发,Day27(复习, defaultdict,Counter,时间模块,random模块,sys模块)

    一.复习 看下面一段代码,假如运行结果有问题,那么就需要在每一步计算时,打印一下结果 b = 1 c = 2 d = 3 a = b+c print(a) e = a + d print(e) 执行输 ...

  7. python常用模块: random模块, time模块, sys模块, os模块, 序列化模块

    一. random模块  import random # 任意小数 print(random.random()) # 0到1的任意小数 print(random.uniform(-10, 10)) # ...

  8. Python基础-os和sys模块

    os模块提供对操作系统进行调用的接口 import os os.getcwd() # 获取当前工作目录 os.chdir(r'd:\fansik') # 修改对当前工作目录 print(os.curd ...

  9. Python之常用模块(re,时间,random,os,sys,序列化模块)(Day20)

    一.时间模块 #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.time() 获取当前时间戳 在Python中表示时间的三种方式:时间戳,元组(s ...

随机推荐

  1. JavaScript设计模式 - 状态模式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 苹果与Windows双系统时间不同步的解决办法

    步骤:打开C盘>Windows>System32,找到cmd.exe,右键以管理员的身份运行. Reg add HKLM\SYSTEM\CurrentControlSet\Control\ ...

  3. python里面 循环明细对比 相同人员明细,生成同一订单里面

    #2018-04-16 def action_create_purc(self,cr,uid,ids,context=None): mrp_origin_obj=self.browse(cr,uid, ...

  4. UOJ14 DZY Loves Graph 并查集

    传送门 题意:给出一张$N$个点,最开始没有边的图,$M$次操作,操作为加入边(边权为当前的操作编号).删除前$K$大边.撤销前一次操作,每一次操作后询问最小生成树边权和.$N \leq 3 \tim ...

  5. NIO之缓冲区

    NIO引入了三个概念: Buffer 缓冲区 Channel 通道 selector 选择器 1.java.io优化建议 操作系统与Java基于流的I/O模型有些不匹配.操作系统要移动的是大块数据(缓 ...

  6. springboot 读取 yml 配置的几种方式

    前言:在springboot 项目中一般默认的配置文件是application.properties,但是实际项目中我们一般会使用application.yml 文件,下面就介绍一下在springbo ...

  7. @Vue/Cli 3 关于 render 空的处理

    问题场景 vue-cli 3 在打包部署时候会出现 dist folder not working "Uncaught TypeError: Cannot set property 'ren ...

  8. ABP+AdminLTE+Bootstrap Table权限管理系统第九节--AdminLTE引入及模板页和布局和菜单

    返回总目录:ABP+AdminLTE+Bootstrap Table权限管理系统一期 AdminLTE AdminLTE 官网地址:https://adminlte.io/themes/AdminLT ...

  9. 【nodejs】让nodejs像后端mvc框架(asp.net mvc)一样处理请求--请求处理结果适配篇(7/8)

    文章目录 前情概要 前面一大坨一大坨的代码把route.controller.action.attribute都搞完事儿了,最后剩下一部分功能就是串起来的调用. 那接下就说个说第二个中间件,也是最后一 ...

  10. C#编写WINNT服务,随便解决安卓开发遇到的5037被众多程序无节操占用的问题

    需求分析: 最近重新开始学习安卓开发,好久不用的ADT集成开发环境频繁遇到不能在仿真机和真机上调试的问题,也就是本人另一篇博文描述的ADB(Android Debug Bridge)监控的5037被金 ...