{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# sys 模块简介" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import sys" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 命令行参数" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`sys.argv` 显示传入的参数:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing print_args.py\n" ] } ], "source": [ "%%writefile print_args.py\n", "import sys\n", "print sys.argv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "运行这个程序:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['print_args.py', '1', 'foo']\n" ] } ], "source": [ "%run print_args.py 1 foo" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "第一个参数 (`sys.args[0]`) 表示的始终是执行的文件名,然后依次显示传入的参数。\n", "\n", "删除刚才生成的文件:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import os\n", "os.remove('print_args.py')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 异常消息" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`sys.exc_info()` 可以显示 `Exception` 的信息,返回一个 `(type, value, traceback)` 组成的三元组,可以与 `try/catch` 块一起使用: " ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(, ZeroDivisionError('integer division or modulo by zero',), )\n" ] } ], "source": [ "try:\n", " x = 1/0\n", "except Exception:\n", " print sys.exc_info()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`sys.exc_clear()` 用于清除所有的异常消息。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 标准输入输出流" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- sys.stdin\n", "- sys.stdout\n", "- sys.stderr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 退出Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`sys.exit(arg=0)` 用于退出 Python。`0` 或者 `None` 表示正常退出,其他值表示异常。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Python Path" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`sys.path` 表示 Python 搜索模块的路径和查找顺序:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['',\n", " 'C:\\\\Anaconda\\\\python27.zip',\n", " 'C:\\\\Anaconda\\\\DLLs',\n", " 'C:\\\\Anaconda\\\\lib',\n", " 'C:\\\\Anaconda\\\\lib\\\\plat-win',\n", " 'C:\\\\Anaconda\\\\lib\\\\lib-tk',\n", " 'C:\\\\Anaconda',\n", " 'C:\\\\Anaconda\\\\lib\\\\site-packages',\n", " 'C:\\\\Anaconda\\\\lib\\\\site-packages\\\\Sphinx-1.3.1-py2.7.egg',\n", " 'C:\\\\Anaconda\\\\lib\\\\site-packages\\\\cryptography-0.9.1-py2.7-win-amd64.egg',\n", " 'C:\\\\Anaconda\\\\lib\\\\site-packages\\\\win32',\n", " 'C:\\\\Anaconda\\\\lib\\\\site-packages\\\\win32\\\\lib',\n", " 'C:\\\\Anaconda\\\\lib\\\\site-packages\\\\Pythonwin',\n", " 'C:\\\\Anaconda\\\\lib\\\\site-packages\\\\setuptools-17.1.1-py2.7.egg',\n", " 'C:\\\\Anaconda\\\\lib\\\\site-packages\\\\IPython\\\\extensions']" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.path" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "在程序中可以修改,添加新的路径。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 操作系统信息" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`sys.platform` 显示当前操作系统信息:\n", "\n", "- `Windows: win32`\n", "- `Mac OSX: darwin`\n", "- `Linux: linux2`" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'win32'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.platform" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "返回 `Windows` 操作系统的版本:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "sys.getwindowsversion(major=6, minor=2, build=9200, platform=2, service_pack='')" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.getwindowsversion()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "标准库中有 `planform` 模块提供更详细的信息。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Python 版本信息" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 16:44:52) [MSC v.1500 64 bit (AMD64)]'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.version" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "sys.version_info(major=2, minor=7, micro=10, releaselevel='final', serial=0)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.version_info" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }