4Python标准库系列之sys模块

Python标准库系列之sys模块

This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It is always available.



sys模块用于提供对解释器相关的操作

模块方法 解释说明
sys.argv 传递到Python脚本的命令行参数列表,第一个元素是程序本身路径
sys.executable 返回Python解释器在当前系统中的绝对路径
sys.exit([arg]) 程序中间的退出,arg=0为正常退出
sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
sys.platform 返回操作系统平台名称,Linux是linux2,Windows是win32
sys.stdout.write(str) 输出的时候把换行符\n去掉
val = sys.stdin.readline()[:-1] 拿到的值去掉\n换行符
sys.version 获取Python解释程序的版本信息
  • 位置参数
[[email protected] ~]# cat scripts.py    
#!/usr/bin/env python
import sys
print(sys.argv[0])
print(sys.argv[1])
print(sys.argv[2])
[[email protected] ~]# python scripts.py canshu1 canshu2  
scripts.py
canshu1
canshu

sys.argv[0]代表脚本本身,如果用相对路径执行则会显示脚本的名称,如果是绝对路径则会显示脚本名称;

  • 程序中途退出

python在默认执行脚本的时候会由头执行到尾,然后自动退出,但是如果需要中途退出程序, 你可以调用sys.exit函数,它带有一个可选的整数参数返回给调用它的程序. 这意味着你可以在主程序中捕获对sys.exit的调用。(注:0是正常退出,其他为不正常,可抛异常事件供捕获!)

原脚本和输出的结果:

[[email protected] sys]# cat sys-03.py 
#!/usr/bin/python
# _*_ coding:utf-8 _*_

import sys

print "hello word!"
print "your is pythoner"
[[email protected] sys]# python sys-03.py 
hello word!
your is pythoner

执行脚本之后会输出,下面这两段内容:

hello word!
your is pythoner

然后我们在print "hello word!"之后让程序退出不执行print "your is pythoner"

[[email protected] sys]# cat sys-03.py 
#!/usr/bin/python
# _*_ coding:utf-8 _*_

import sys

print "hello word!"
sys.exit()
print "your is pythoner"
[[email protected] sys]# python sys-03.py 
hello word!

PS:sys.exit从python程序中退出,将会产生一个systemExit异常,可以为此做些清除除理的工作。这个可选参数默认正常退出状态是0,以数值为参数的范围为:0-127。其他的数值为非正常退出,还有另一种类型,在这里展现的是strings对象类型。

  • 获取模块路径

在使用Python中用import_import_导入模块的时候,那Python是怎么判断有没有这个模块的呢?
其实就是根据sys.path的路径来搜索你导入模块的名称。

 >>> for i in sys.path:
 ...  print(i)
 ...
 
C:\Python35\lib\site-packages\pip-8.1.1-py3.5.egg
C:\Python35\python35.zip
C:\Python35\DLLs
C:\Python35\lib
C:\Python35
C:\Python35\lib\site-packages
  • 获取当前系统平台

Linux

 >>> import sys
 >>> sys.platform
‘linux2‘

Windows

 >>> import sys
 >>> sys.platform
‘win32‘

#Python标准库 #Sys

时间: 2024-08-08 07:40:10

4Python标准库系列之sys模块的相关文章

22Python标准库系列之Redis模块

Python标准库系列之Redis模块 What is redis? Redis is an open source (BSD licensed), in-memory data structure store, used as database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, b

11Python标准库系列之configparser模块

Python标准库系列之configparser模块 This module provides the ConfigParser class which implements a basic configuration language which provides a structure similar to what's found in Microsoft Windows INI files. You can use this to write Python programs which

9Python标准库系列之time模块

Python标准库系列之time模块 This module provides various functions to manipulate time values. 方法名 说明 time.sleep(int) 等待时间 time.time() 输出时间戳,从1970年1月1号到现在用了多少秒 time.ctime() 返回当前的系统时间 time.gmtime() 将时间戳转换成struct_time格式 time.localtime() 以struct_time格式返回本地时间 time

12Python标准库系列之subprocess模块

Python标准库系列之subprocess模块 This module allows you to spawn processes, connect to their input/output/error pipes, and obtain their return codes. 常用方法实例 call() 执行命令,并返回状态码,状态码0代表命令执行成功,其他的都表示命令执行不成功 >>> ret = subprocess.call(["ls", "-l

7Python标准库系列之requests模块

Python标准库系列之requests模块 Requests is the only Non-GMO HTTP library for Python, safe for human consumption. 官方文档:http://docs.python-requests.org/en/master/ 安装Requests模块 Requests模块官方提供了两种方式安装: pip方式安装 pip install requests 源码方式安装 git clone git://github.co

5Python标准库系列之json模块

Python标准库系列之json模块 JSON (JavaScript Object Notation) http://json.org is a subset of JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data interchange format. JSON通常用于在Web客户端和服务器数据交换,即把字符串类型的数据转换成Python基本数据类型或者将Python基本数据类型转换成字符串类型. 常用方法

13Python标准库系列之shutil模块

Python标准库系列之shutil模块 The shutil module offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal. For operations on individual files, see also the os modul

14Python标准库系列之zipfile模块

Python标准库系列之zipfile模块 The ZIP file format is a common archive and compression standard. This module provides tools to create, read, write, append, and list a ZIP file. This module does not currently handle multi-disk ZIP files. It can handle ZIP file

10Python全栈之路系列之深浅拷贝标准库系列之datetime模块

Python标准库系列之datetime模块 Fast implementation of the datetime type. 功能 说明 datetime.date.today() 打印输出当前的系统日期 datetime.date.fromtimestamp(time.time()) 将时间戳转成日期格式 datetime.datetime.now() 打印当前的系统时间 current_time.replace(2016,5,12) 返回当前时间,但指定的值将被替换 datetime.d