python模块----sys模块 (系统相关的参数和函数)
pprint 模块:它给我们提供了一个方法 pprint() 该方法可以用来对打印的数据做简单的格式化
sys模块+pprint模块
- 标准库网址(sys):https://docs.python.org/3/library/sys.html#module-sys
- 标准库网址(pprint):https://docs.python.org/3/library/pprint.html#module-pprint
- sys.argv
返回一个列表,列表中保存了当前命令的所有参数,argv[0] 为脚本的名称(是否是完整的路径名取决于操作系统)
import sys
import pprint
print(sys.argv)
print(sys.argv[0])
print(sys.argv[1])
print(sys.argv[1:])
- sys.modules
返回一个字典,获取当前程序中引入的所有模块;字典的key是模块的名字,字典的value是模块对象
pprint.pprint(sys.modules)
print(sys.modules.keys())
print(sys.modules.values())
print(sys.modules['os'])
- sys.path
他是一个列表,列表中保存的是模块的搜索路径
pprint.pprint(sys.path)
- sys.platform
表示当前Python运行的平台
print(sys.platform)
- sys.exit()
执行到主程序末尾,解释器自动退出,但是如果需要中途退出程序,可以调用sys.exit函数,带有一个可选的整数参数返回给调用它的程序,表示你可以在主程序中捕获对sys.exit的调用。
sys.exit('程序出现异常,结束!')
测试
[root@c43i08102.cloud.i09.amtest90 /home]
#cat systest.py
import sys
import pprint
print("------------------------------------")
pprint.pprint(sys.path)
print("------------------------------------")
pprint.pprint(sys.argv)
print("------------------------------------")
pprint.pprint(sys.modules)
print("------------------------------------")
pprint.pprint(sys.platform)
print("------------------------------------")
sys.exit("ERROR!!EXIT")
print("-------test exit--------")
[root@c43i08102.cloud.i09.amtest90 /home]
#python systest.py
------------------------------------
['/home',
'/usr/local/lib/python2.7/site-packages/setuptools-20.1.1-py2.7.egg',
'/usr/local/lib/python2.7/site-packages/pip-8.0.3-py2.7.egg',
'/usr/local/lib/python27.zip',
'/usr/local/lib/python2.7',
'/usr/local/lib/python2.7/plat-linux2',
'/usr/local/lib/python2.7/lib-tk',
'/usr/local/lib/python2.7/lib-old',
'/usr/local/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/site-packages']
------------------------------------
['systest.py']
------------------------------------
{'UserDict': <module 'UserDict' from '/usr/local/lib/python2.7/UserDict.pyc'>,
'__builtin__': <module '__builtin__' (built-in)>,
'__main__': <module '__main__' from 'systest.py'>,
'_abcoll': <module '_abcoll' from '/usr/local/lib/python2.7/_abcoll.pyc'>,
'_codecs': <module '_codecs' (built-in)>,
'_locale': <module '_locale' from '/usr/local/lib/python2.7/lib-dynload/_locale.so'>,
'_sre': <module '_sre' (built-in)>,
'_sysconfigdata': <module '_sysconfigdata' from '/usr/local/lib/python2.7/_sysconfigdata.pyc'>,
'_warnings': <module '_warnings' (built-in)>,
'_weakref': <module '_weakref' (built-in)>,
'_weakrefset': <module '_weakrefset' from '/usr/local/lib/python2.7/_weakrefset.pyc'>,
'abc': <module 'abc' from '/usr/local/lib/python2.7/abc.pyc'>,
'cStringIO': <module 'cStringIO' from '/usr/local/lib/python2.7/lib-dynload/cStringIO.so'>,
'codecs': <module 'codecs' from '/usr/local/lib/python2.7/codecs.pyc'>,
'copy_reg': <module 'copy_reg' from '/usr/local/lib/python2.7/copy_reg.pyc'>,
'encodings': <module 'encodings' from '/usr/local/lib/python2.7/encodings/__init__.pyc'>,
'encodings.__builtin__': None,
'encodings.aliases': <module 'encodings.aliases' from '/usr/local/lib/python2.7/encodings/aliases.pyc'>,
'encodings.codecs': None,
'encodings.encodings': None,
'encodings.utf_8': <module 'encodings.utf_8' from '/usr/local/lib/python2.7/encodings/utf_8.pyc'>,
'errno': <module 'errno' (built-in)>,
'exceptions': <module 'exceptions' (built-in)>,
'genericpath': <module 'genericpath' from '/usr/local/lib/python2.7/genericpath.pyc'>,
'linecache': <module 'linecache' from '/usr/local/lib/python2.7/linecache.pyc'>,
'os': <module 'os' from '/usr/local/lib/python2.7/os.pyc'>,
'os.path': <module 'posixpath' from '/usr/local/lib/python2.7/posixpath.pyc'>,
'posix': <module 'posix' (built-in)>,
'posixpath': <module 'posixpath' from '/usr/local/lib/python2.7/posixpath.pyc'>,
'pprint': <module 'pprint' from '/usr/local/lib/python2.7/pprint.pyc'>,
're': <module 're' from '/usr/local/lib/python2.7/re.pyc'>,
'signal': <module 'signal' (built-in)>,
'site': <module 'site' from '/usr/local/lib/python2.7/site.pyc'>,
'sre_compile': <module 'sre_compile' from '/usr/local/lib/python2.7/sre_compile.pyc'>,
'sre_constants': <module 'sre_constants' from '/usr/local/lib/python2.7/sre_constants.pyc'>,
'sre_parse': <module 'sre_parse' from '/usr/local/lib/python2.7/sre_parse.pyc'>,
'stat': <module 'stat' from '/usr/local/lib/python2.7/stat.pyc'>,
'sys': <module 'sys' (built-in)>,
'sysconfig': <module 'sysconfig' from '/usr/local/lib/python2.7/sysconfig.pyc'>,
'traceback': <module 'traceback' from '/usr/local/lib/python2.7/traceback.pyc'>,
'types': <module 'types' from '/usr/local/lib/python2.7/types.pyc'>,
'warnings': <module 'warnings' from '/usr/local/lib/python2.7/warnings.pyc'>,
'zipimport': <module 'zipimport' (built-in)>}
------------------------------------
'linux2'
------------------------------------
ERROR!!EXIT
python模块----sys模块 (系统相关的参数和函数)的更多相关文章
- python 全栈开发,Day27(复习, defaultdict,Counter,时间模块,random模块,sys模块)
一.复习 看下面一段代码,假如运行结果有问题,那么就需要在每一步计算时,打印一下结果 b = 1 c = 2 d = 3 a = b+c print(a) e = a + d print(e) 执行输 ...
- python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则
python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess ...
- python之sys模块详解
python之sys模块详解 sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧! sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传 ...
- python之sys模块
38.python的sys模块: 用于提供对Python解释器相关的操作: 1 2 3 4 5 6 7 8 9 sys.argv 命令行参数List,第一个元素是程序本身路径 sy ...
- Python模块02/序列化/os模块/sys模块/haslib加密/collections
Python模块02/序列化/os模块/sys模块/haslib加密/collections 内容大纲 1.序列化 2.os模块 3.sys模块 4.haslib加密 5.collections 1. ...
- Python进阶(九)----json模块, pickle模块, os模块,sys模块,hashlib模块
Python进阶----json模块, pickle模块, os模块,sys模块,hashlib模块 一丶序列化模块 什么是序列化: 将一种数据结构,转换成一个特殊的序列(特殊字符串,用于网络传输 ...
- Python中sys模块
Python的sys模块提供访问解释器使用或维护的变量,和与解释器进行交互的函数.通俗来讲,sys模块负责程序与python解释器的交互,提供了一系列的函数和变量,用于操控python运行时的环境. ...
- shelve模块,sys模块,logging模块
1.shelve模块 用于序列化的模块,shelve模块比pickle模块简单,只有open函数,返回类似字典的对象,可读可写;key必须为字符串,而值可以是python所支持的数据类型. impor ...
- day13 函数模块之序列化 random 模块 os模块 sys模块 hashlib模块 collections模块
json import json dic = {'k1':'v1','k2':'v2','k3':'v3'} str_dic = json.dumps(dic) #序列化:将一个字典转换成一个字符串 ...
随机推荐
- C++模板元编程----堆排序
目录 目录 前言 实现的一些小细节 Debug 惰性求值 总结 Ref 前言 经过前两次经验的积累,终于来到了麻烦的堆排序.在一开始接触模板元编程的时候,我就期望有一天能够写出元编程堆排序的代码.原因 ...
- LOG_PATH_IS_UNDEFINED
问题: SpringBoot + LogBack 写日志,除了正常的日志之外,程序跑起来后会在项目根目录多生成一个 LOG_PATH_IS_UNDEFINED 的文件夹,我的配置是以下: applic ...
- windows中关闭端口的方法
打开cmd:输入-ano | findstr "端口号" 控制台会输出占用端口的pid 如"8380" 再向cmd中输入 taskkill /f/pid 838 ...
- [每日一题]面试官问:Async/Await 如何通过同步的方式实现异步?
关注「松宝写代码」,精选好文,每日一题 时间永远是自己的 每分每秒也都是为自己的将来铺垫和增值 作者:saucxs | songEagle 一.前言 2020.12.23 日刚立的 flag,每日一 ...
- js--实现限制input输入框数字输入,实现每四位一个空格效果(银行卡号,手机号等)
前言 工作学习中经常能遇到输入框限制输入数字,并且每四位一空格的情况,比如表单中银行卡号,手机号等输入框的限制,这里介绍一下使用js具体的实现方法.不需要引用第三方ui库. 正文 1.input标签的 ...
- ICMP协议概述
• ICMP是三层协议,和IP.ARP.ICMP同属三层 • IP协议中的6是代表上层的TCP协议,17代表UDP协议,1代表同层的ICMP协议 • ICMP协议主要用来探测 ...
- 每日CSS_滚动页面动画效果
每日CSS_滚动页面动画效果 2021_1_13 源码链接 1. 代码解析 1.1 html 代码片段 <section> <h2>开 始 滑 动</h2> < ...
- springboot 和 mongdb连接问题 Exception in thread "main" com.mongodb.MongoSecurityException:
1 Exception in thread "main" com.mongodb.MongoSecurityException: Exception authenticating ...
- mysql: Character set 'utf8mb4' is not a compiled character set and is not specified in the '/usr/share/mysql/charsets/Index.xml' file
mysql: Character set 'utf8mb4' is not a compiled character set and is not specified in the '/usr/sha ...
- 以事实驳斥:改进你的c#代码的5个技巧(四)
测试使用的环境:vs2019+.net core3.1 原文地址:https://www.cnblogs.com/hhhnicvscs/p/14296715.html 反驳第一条:如何检查代码中的空字 ...