Pythone3 sys模块
1.sys.argv 可以实现从程序外部向程序传递参数
2.sys.exit() 程序中间退出,exit(0)正常退出,其他为异常退出
3.sys.getdefaultencoding() 获取系统编码方式
4.sys.setdefaultencoding() 设置系统编码方式
5.sys.getfilesystemencoding() 获取文件系统编码方式
6.sys.path 获取系统环境变量中PATH的路径字符串
7.sys.path.append("路径") 追加路径到系统环境变量PATH的后面
8.sys.path.insert("路径") 将路径插入到系统环境变量PATH的最前面
9.sys.platform: 获取当前系统平台。
10.sys.modules 是一个全局字典,该字典是python启动后就加载在内存中。每当程序员导入新的模块,sys.modules将自动记录该模块。当第二次再导入该模块时,python会直接到字典中查找,从而加快了程序运行的速度。它拥有字典所拥有的一切方法。
1
2
3
|
print (sys.modules.keys()) print (sys.modules.values()) print (sys.modules[ "os" ]) |
输出结果:
1
2
3
|
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' ]) 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 'D:\\Programs\\Python\\Python37\\lib\\encodings\\__init__.py' >, <module 'codecs' from 'D:\\Programs\\Python\\Python37\\lib\\codecs.py' >, <module '_codecs' (built - in )>, <module 'encodings.aliases' from 'D:\\Programs\\Python\\Python37\\lib\\encodings\\aliases.py' >, <module 'encodings.utf_8' from 'D:\\Programs\\Python\\Python37\\lib\\encodings\\utf_8.py' >, <module '_signal' (built - in )>, <module '__main__' from 'D:/Programs/Python/PycharmProjects/OldManS14/day05/module/sys_模块.py' >, <module 'encodings.latin_1' from 'D:\\Programs\\Python\\Python37\\lib\\encodings\\latin_1.py' >, <module 'io' from 'D:\\Programs\\Python\\Python37\\lib\\io.py' >, <module 'abc' from 'D:\\Programs\\Python\\Python37\\lib\\abc.py' >, <module '_abc' (built - in )>, <module 'site' from 'D:\\Programs\\Python\\Python37\\lib\\site.py' >, <module 'os' from 'D:\\Programs\\Python\\Python37\\lib\\os.py' >, <module 'stat' from 'D:\\Programs\\Python\\Python37\\lib\\stat.py' >, <module '_stat' (built - in )>, <module 'ntpath' from 'D:\\Programs\\Python\\Python37\\lib\\ntpath.py' >, <module 'genericpath' from 'D:\\Programs\\Python\\Python37\\lib\\genericpath.py' >, <module 'ntpath' from 'D:\\Programs\\Python\\Python37\\lib\\ntpath.py' >, <module '_collections_abc' from 'D:\\Programs\\Python\\Python37\\lib\\_collections_abc.py' >, <module '_sitebuiltins' from 'D:\\Programs\\Python\\Python37\\lib\\_sitebuiltins.py' >, <module '_bootlocale' from 'D:\\Programs\\Python\\Python37\\lib\\_bootlocale.py' >, <module '_locale' (built - in )>, <module 'encodings.gbk' from 'D:\\Programs\\Python\\Python37\\lib\\encodings\\gbk.py' >, <module '_codecs_cn' (built - in )>, <module '_multibytecodec' (built - in )>, <module 'encodings.cp437' from 'D:\\Programs\\Python\\Python37\\lib\\encodings\\cp437.py' >, <module 'sitecustomize' from 'D:\\Programs\\Python\\PyCharm 2018.1.4\\helpers\\pycharm_matplotlib_backend\\sitecustomize.py' >]) <module 'os' from 'D:\\Programs\\Python\\Python37\\lib\\os.py' > |
11. stdin , stdout , 以及stderr 变量包含与标准I/O 流对应的流对象. 如果需要更好地控制输出,,而print 不能满足你的要求, 它们就是你所需要的. 你也可以替换它们, 这时候你就可以重定向输出和输入到其它设备( device ), 或者以非标准的方式处理它们
1
2
3
|
sys.stderr.write( "stderr" ) print ( "=======" ) sys.stdout.write( "stdout" ) |
输出结果:
1
2
|
stderr = = = = = = = stdout |