别院牧志知识库 别院牧志知识库
首页
  • 基础

    • 全栈之路
    • 😎Awesome资源
  • 进阶

    • Python 工匠系列
    • 高阶知识点
  • 指南教程

    • Socket 编程
    • 异步编程
    • PEP 系列
  • 面试

    • Python 面试题
    • 2025 面试记录
    • 2022 面试记录
    • 2021 面试记录
    • 2020 面试记录
    • 2019 面试记录
    • 数据库索引原理
  • 基金

    • 基金知识
    • 基金经理
  • 细读经典

    • 德隆-三个知道
    • 孔曼子-摊大饼理论
    • 配置者说-躺赢之路
    • 资水-建立自己的投资体系
    • 反脆弱
  • Git 参考手册
  • 提问的智慧
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
首页
  • 基础

    • 全栈之路
    • 😎Awesome资源
  • 进阶

    • Python 工匠系列
    • 高阶知识点
  • 指南教程

    • Socket 编程
    • 异步编程
    • PEP 系列
  • 面试

    • Python 面试题
    • 2025 面试记录
    • 2022 面试记录
    • 2021 面试记录
    • 2020 面试记录
    • 2019 面试记录
    • 数据库索引原理
  • 基金

    • 基金知识
    • 基金经理
  • 细读经典

    • 德隆-三个知道
    • 孔曼子-摊大饼理论
    • 配置者说-躺赢之路
    • 资水-建立自己的投资体系
    • 反脆弱
  • Git 参考手册
  • 提问的智慧
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 辨析

  • Sockets编程

  • Django

  • stackoverflow

  • Flask

  • 全栈之路

    • 基础篇

    • 进阶篇

    • 面向对象

    • 网络编程

    • 操作系统

    • 标准库

      • Python 标准库系列之 collections 模块
      • Python 标准库系列之 configparser 模块
      • Python 标准库系列之 datetime 模块
      • Python 标准库系列之 hashlib 模块
      • Python 标准库系列之模块介绍
      • Python 标准库系列之 json 模块
      • Python 标准库系列之 logging 模块
      • Python 标准库系列之 os 模块
      • Python 标准库系列之 random 模块
      • Python 标准库系列之 shutil 模块
      • Python 标准库系列之 subprocess 模块
      • Python 标准库系列之 sys 模块
      • Python 标准库系列之 tarfile 模块
      • Python 标准库系列之 time 模块
      • Python 标准库系列之 xml 模块
      • Python 标准库系列之 zipfile 模块
    • 第三方库

    • Python 全栈之路
  • 面试

  • 代码片段

  • 异步编程

  • 😎Awesome资源

  • PEP

  • Python工匠系列

  • 高阶知识点

  • Python 学习资源待整理
  • 设计模式

  • 好“艹蛋”的 Python 呀!
  • FIFO | 待学清单📝
  • pip 安装及使用
  • 数据分析

  • 源码阅读计划

  • OOP

  • 关于 python 中的 setup.py
  • 并行分布式框架 Celery
  • 七种武器,让你的代码提高可维护性
  • 使用 pdb 调试 Python 代码
  • 每周一个 Python 标准库
  • 🐍Python
  • 全栈之路
  • 标准库
佚名
2020-05-23

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 解释程序的版本信息
  • 位置参数
[root@ansheng ~]# cat scripts.py    
#!/usr/bin/env python
import sys
print(sys.argv[0])
print(sys.argv[1])
print(sys.argv[2])
[root@ansheng ~]# python scripts.py canshu1 canshu2  
scripts.py
canshu1
canshu2
1
2
3
4
5
6
7
8
9
10

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

  • 程序中途退出

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

原脚本和输出的结果:

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

import sys

print "hello word!"
print "your is pythoner"
[root@iZ28i253je0Z sys]# python sys-03.py 
hello word!
your is pythoner
1
2
3
4
5
6
7
8
9
10
11

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

hello word!
your is pythoner
1
2

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

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

import sys

print "hello word!"
sys.exit()
print "your is pythoner"
[root@iZ28i253je0Z sys]# python sys-03.py 
hello word!
1
2
3
4
5
6
7
8
9
10
11

**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
1
2
3
4
5
6
7
8
9
10
  • 获取当前系统平台

Linux

 >>> import sys
 >>> sys.platform
'linux2'
1
2
3

Windows

 >>> import sys
 >>> sys.platform
'win32'
1
2
3
编辑 (opens new window)
#编码#面向对象
上次更新: 2024-07-23, 01:00:43
Python 标准库系列之 subprocess 模块
Python 标准库系列之 tarfile 模块

← Python 标准库系列之 subprocess 模块 Python 标准库系列之 tarfile 模块→

最近更新
01
2025 面试记录
05-28
02
提升沟通亲和力的实用策略
03-26
03
工作
07-15
更多文章>
Theme by Vdoing | Copyright © 2019-2025 IMOYAO | 别院牧志
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式