# sys模塊
`sys`模塊包含系統對應的功能。我們已經學習了`sys.argv`列表,它包含命令行參數。
```
#!/usr/bin/python
# Filename: cat.py
import sys
def readfile(filename):
????'''Print a file to the standard output.'''
????f = file(filename)
????while True:
????????line = f.readline()
????????if len(line) == 0:
????????????break
????????print line, # notice comma
????f.close()
# Script starts from here
if len(sys.argv) < 2:
????print 'No action specified.'
????sys.exit()
if sys.argv[1].startswith('--'):
????option = sys.argv[1][2:]
????# fetch sys.argv[1] but without the first two characters
????if option == 'version':
????????print 'Version 1.2'
????elif option == 'help':
????????print '''\
This program prints files to the standard output.
Any number of files can be specified.
Options include:
??--version : Prints the version number
??--help??? : Display this help'''
????else:
????????print 'Unknown option.'
????sys.exit()
else:
????for filename in sys.argv[1:]:
????????readfile(filename)
```
(源文件:[code/cat.py](code/cat.py))
## 輸出
```
$ python cat.py
No action specified.
$ python cat.py --help
This program prints files to the standard output.
Any number of files can be specified.
Options include:
??--version : Prints the version number
??--help??? : Display this help
$ python cat.py --version
Version 1.2
$ python cat.py --nonsense
Unknown option.
$ python cat.py poem.txt
Programming is fun
When the work is done
if you wanna make your work also fun:
????????use Python!
```
## 它如何工作
這個程序用來模范Linux/Unix用戶熟悉的**cat**命令。你只需要指明某些文本文件的名字,這個程序會把它們打印輸出。
在Python程序運行的時候,即不是在交互模式下,在`sys.argv`列表中總是至少有一個項目。它就是當前運行的程序名稱,作為`sys.argv[0]`(由于Python從`0`開始計數)。其他的命令行參數在這個項目之后。
為了使這個程序對用戶更加友好,我們提供了一些用戶可以指定的選項來了解更多程序的內容。我們使用第一個參數來檢驗我們的程序是否被指定了選項。如果使用了`--version`選項,程序的版本號將被打印出來。類似地,如果指定了`--help`選項,我們提供一些關于程序的解釋。我們使用`sys.exit`函數退出正在運行的程序。和以往一樣,你可以看一下`help(sys.exit)`來了解更多詳情。
如果沒有指定任何選項,而是為程序提供文件名的話,它就簡單地打印出每個文件地每一行,按照命令行中的順序一個文件接著一個文件地打印。
順便說一下,名稱**cat**是 concatenate 的縮寫,它基本上表明了程序的功能——它可以在輸出打印一個文件或者把兩個或兩個以上文件連接/級連在一起打印。
`sys.version`字符串給你提供安裝的Python的版本信息。`sys.version_info`元組則提供一個更簡單的方法來使你的程序具備Python版本要求功能。
```
[swaroop@localhost code]$ python
>>> import sys
>>> sys.version
'2.3.4 (#1, Oct 26 2004, 16:42:40) \n[GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)]'
>>> sys.version_info
(2, 3, 4, 'final', 0)
```
對于有經驗的程序員,`sys`模塊中其他令人感興趣的項目有`sys.stdin`、`sys.stdout`和`sys.stderr`它們分別對應你的程序的標準輸入、標準輸出和標準錯誤流。
- 版權信息
- 前言
- 本書的由來
- 本書目前的狀況
- 約定條款
- 反饋
- 值得思考的一些東西
- 第1章 介紹
- Python的特色
- 為什么不使用Perl?
- 程序員的話
- 第2章 安裝Python
- Windows?用戶
- 概括
- 第3章 最初的步驟
- 使用帶提示符的解釋器
- 挑選一個編輯器
- 使用源文件
- 可執行的Python程序
- 獲取幫助
- 概括
- 第4章 基本概念
- 數
- 字符串
- 變量
- 標識符的命名
- 數據類型
- 對象
- 邏輯行與物理行
- 縮進
- 概括
- 第5章 運算符與表達式
- 運算符
- 運算符優先級
- 表達式
- 概括
- 第6章 控制流
- if語句
- while語句
- for循環
- break語句
- continue語句
- 概括
- 第7章 函數
- 函數形參
- 局部變量
- 默認參數值
- 關鍵參數
- return語句
- DocStrings
- 概括
- 第8章 模塊
- 字節編譯的.pyc文件
- from..import語句
- 模塊的name
- 制造你自己的模塊
- dir()函數
- 概括
- 第9章 數據結構
- 列表
- 元組
- 字典
- 序列
- 參考
- 更多字符串的內容
- 概括
- 第10章 解決問題——編寫一個Python腳本
- 解決方案
- 軟件開發過程
- 概括
- 第11章 面向對象的編程
- self
- 類
- 對象的方法
- __init__方法
- 類與對象的方法
- 繼承
- 概括
- 第12章 輸入/輸出
- 儲存器
- 概括
- 第13章 異常
- try..except
- 引發異常
- try..finally
- 概括
- 第14章 Python標準庫
- sys模塊
- os模塊
- 概括
- 第15章 更多Python的內容
- 單語句塊
- 列表綜合
- 在函數中接收元組和列表
- lambda形式
- exec和eval語句
- assert語句
- repr函數
- 概括
- 第16章 接下來學習什么?
- 探索更多內容
- 概括
- 附錄A 自由/開放源碼軟件(FLOSS)
- 附錄B 關于本書
- 關于作者
- 關于譯者
- 關于簡體中文譯本
- 附錄C 修訂記錄
- 術語表