博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【转载】Python常用模块之sys
阅读量:6867 次
发布时间:2019-06-26

本文共 3123 字,大约阅读时间需要 10 分钟。

sys模块提供了一系列有关Python运行环境的变量和函数。

常见用法

sys.argv
可以用sys.argv获取当前正在执行的命令行参数的参数列表(list)。
变量 解释
sys.argv[0] 当前程序名
sys.argv[1] 第一个参数
sys.argv[0] 第二个参数


参考代码:

# encoding: utf-8# filename: argv_test.pyimport sys# 获取脚本名字print 'The name of this program is: %s' %(sys.argv[0])# 获取参数列表print 'The command line arguments are:'for i in sys.argv:    print i# 统计参数个数print 'There are %s arguments.'%(len(sys.argv)-1)

运行结果:

E:\p>python argv_test.py arg1 arg2 arg3The name of this program is: argv_test.pyThe command line arguments are:argv_test.pyarg1arg2arg3There are 3 arguments.

sys.platform

获取当前执行环境的平台,如win32表示是Windows 32bit操作系统,linux2表示是linux平台;

# linux >>> import sys>>> sys.platform'linux2'# windows>>> import sys>>> sys.platform'win32'

sys.path

path是一个目录列表,供Python从中查找第三方扩展模块。在python启动时,sys.path根据内建规则、PYTHONPATH变量进行初始化。

 
>>> sys.path['', 'E:\\Python27\\Lib\\idlelib', 'C:\\Windows\\system32\\python27.zip', 'E:\\Python27\\DLLs', 'E:\\Python27\\lib', 'E:\\Python27\\lib\\plat-win', 'E:\\Python27\\lib\\lib-tk', 'E:\\Python27', 'E:\\Python27\\lib\\site-packages']
**有时候为了让python能够找到我们自己定义的模块,需要修改sys.path的内容,比如:**
# 在path的开始位置 插入test>>> sys.path.insert(0,'test')>>> sys.path['test', '', 'E:\\Python27\\Lib\\idlelib', 'C:\\Windows\\system32\\python27.zip', 'E:\\Python27\\DLLs', 'E:\\Python27\\lib', 'E:\\Python27\\lib\\plat-win', 'E:\\Python27\\lib\\lib-tk', 'E:\\Python27', 'E:\\Python27\\lib\\site-packages']# 可以成功import test>>> import test# 找不到 other 这个模块>>> import otherTraceback (most recent call last):  File "
", line 1, in
import otherImportError: No module named other# 需要添加path>>> sys.path.insert(0,'other')>>> import other
也可以用sys.path.append(“mine module path”)来添加自定义的module。sys.builtin_module_namessys.builtin_module_names返回一个列表,包含内建模块的名字。如:>>> import sys>>> print sys.builtin_module_names('__builtin__', '__main__', '_ast', '_bisect', '_codecs', '_codecs_cn', '_codecs_hk', '_codecs_iso2022', '_codecs_jp', '_codecs_kr', '_codecs_tw', '_collections', '_csv', '_functools', '_heapq', '_hotshot', '_io', '_json', '_locale', '_lsprof', '_md5', '_multibytecodec', '_random', '_sha', '_sha256', '_sha512', '_sre', '_struct', '_subprocess', '_symtable', '_warnings', '_weakref', '_winreg', 'array', 'audioop', 'binascii', 'cPickle', 'cStringIO', 'cmath', 'datetime', 'errno', 'exceptions', 'future_builtins', 'gc', 'imageop', 'imp', 'itertools', 'marshal', 'math', 'mmap', 'msvcrt', 'nt', 'operator', 'parser', 'signal', 'strop', 'sys', 'thread', 'time', 'xxsubtype', 'zipimport', 'zlib')代码示例:# encoding: utf-8# find_module.pyimport sys# print sys.builtin_module_namesdef find_module(module):    if module in sys.builtin_module_names:        print module," => ","__builtin__"    else:        print module,"=> ",__import__(module).__file__find_module('os')find_module('sys')find_module('strop')find_module('zlib')find_module('string')# 运行结果:>>> ======================== RESTART: E:/p/find_module.py ========================os =>  E:\Python27\lib\os.pycsys  =>  __builtin__strop  =>  __builtin__zlib  =>  __builtin__string =>  E:\Python27\lib\string.pyc

转载于:https://blog.51cto.com/dongbian77/2071259

你可能感兴趣的文章
Google Native Client入门
查看>>
spark能传递外部命名参数给main函数吗?
查看>>
[LeetCode] Convex Polygon 凸多边形
查看>>
递归神经网络
查看>>
iframe父页面和子页面相互调用的方法
查看>>
【批处理学习笔记】第十七课:截取字符串
查看>>
[Erlang 0066] Erlang orddict
查看>>
Hadoop HDFS 用户指南
查看>>
体验mssql-cli
查看>>
ASP.NET MVC之国际化(十一)
查看>>
Swift析构器
查看>>
★路由递归查询方法及相关图…
查看>>
SpringMvc入门
查看>>
scrapy 登录
查看>>
上海往事之看房子
查看>>
SQL Server使用规范
查看>>
高性能mysql主存架构
查看>>
《Programming WPF》翻译 第7章 3.笔刷和钢笔
查看>>
[20160906]修改口令在内存中.txt
查看>>
解剖SQLSERVER 第九篇 OrcaMDF现在能通过系统DMVs显示元数据(译)
查看>>