python sys干嘛的_Python之sys模块
Sys模塊函數(shù)之多,我只能選取自己認(rèn)為比較實(shí)用的一些函數(shù)列在此處。借馬云找員工的說法,”找最合適的而不是最天才的”,這句話,我個(gè)人覺得在很多方面都能適應(yīng),學(xué)習(xí)也不在話下。Sys模塊功能的確很多,但我們應(yīng)該將重點(diǎn)放在那些功能才是最適合我們的,為此,我列的這些函數(shù),就是我認(rèn)為比較適合我以后開發(fā)的函數(shù)。
(1)sys.argv
很多人會(huì)想,我如何給我的程序在外部傳遞參數(shù)呢?這個(gè),就可以實(shí)現(xiàn)。如:
Tesy.py
Import sys
Print sys.argv[number]
一般情況下,number為0是這個(gè)腳本的名字,1,2…則為命令行下傳遞的參數(shù).如:
Test.py腳本內(nèi)容:
import sys
print sys.argv[0]
print sys.argv[1]
print sys.argv[2]
print sys.argv[3]
那么
[root@databak scripts]# python test.py arg1 arg2 arg3
test.py
arg1
arg2
arg3
看到,對應(yīng)的關(guān)系了嗎?還有,在python.org模塊參考手冊說,如果在命令行下選用-c那么argv[0]= -c 看下,
[root@databak scripts]# python -c "import sys;print sys.argv[0];print sys.argv[1]" arg1
-c
arg1
如果大家不明白,可以參考下man python
SYNOPSIS
python [ -d ] [ -E ] [ -h ] [ -i ] [ -m module-name ] [ -O ]
[ -Q argument ] [ -S ] [ -t ] [ -u ]
[ -v ] [ -V ] [ -W argument ] [ -x ]
[ -c command | script | - ] [ arguments ]
(2)sys.platform
大家都知道,當(dāng)今的程序比較流行的是跨平臺(tái)。簡單的說就是這段程序既可以在windows下,換到linux下也可以不加修改的運(yùn)行起來,聽起來就不錯(cuò)。所以,這個(gè)函數(shù)就可以派上用場了。
假設(shè),我們想實(shí)現(xiàn)一個(gè)清除終端,linux下用clear, windows下用cls
Ostype=sys.platform()
If ostype==”linux” or ostype==”linux2”:
Cmd=”clear”
Else:
Cmd=”cls”
(3) sys.exit(n)
執(zhí)行至主程序的末尾時(shí),解釋器會(huì)自動(dòng)退出. 但是如果需要中途退出程序, 你可以調(diào)用sys.exit 函數(shù), 它帶有一個(gè)可選的整數(shù)參數(shù)返回給調(diào)用它的程序. 這意味著你可以在主程序中捕獲對sys.exit 的調(diào)用。(注:0是正常退出,其他為不正常,可拋異常事件供捕獲!)
import sys
def exitfunc(value):
'''Clear function'''
print value
sys.exit(0)
print "hello"
try:
sys.exit(1)
except SystemExit,value:
exitfunc(value)
print "come?"
輸出結(jié)果:
[root@databak scripts]# python test.py
hello
1
以下是python.org庫參考手冊中,摘抄來的,供參考。
Exit from Python. This is implemented by raising the SystemExit exception, so cleanup actions specified by finally clauses of try statements are honored, and it is possible to intercept the exit attempt at an outer level. The optional argument arg can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered “successful termination” and any nonzero value is considered “abnormal termination” by shells and the like. Most systems require it to be in the range 0-127, and produce undefined results otherwise. Some systems have a convention for assigning specific meanings to specific exit codes, but these are generally underdeveloped; Unix programs generally use 2 for command line syntax errors and 1 for all other kind of errors. If another type of object is passed, None is equivalent to passing zero, and any other object is printed to sys.stderr and results in an exit code of 1. In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs.
大概意思是說,sys.exit從python程序中退出,將會(huì)產(chǎn)生一個(gè)systemExit異常,可以為此做些清除除理的工作。這個(gè)可選參數(shù)默認(rèn)正常退出狀態(tài)是0,以數(shù)值為參數(shù)的范圍為:0-127。其他的數(shù)值為非正常退出,還有另一種類型,在這里展現(xiàn)的是strings對象類型。
(4)sys.path
大家對模塊都有一定了解吧?大家在使用模塊的某一個(gè)功能前,是不是需要導(dǎo)入呢?答案是需要。那import,__import__命令就不用提干嘛的了吧。那大家在執(zhí)行import module_name的時(shí)候,python內(nèi)部發(fā)生了什么呢?簡單的說,就是搜索module_name。根據(jù)sys.path的路徑來搜索module.name
>>> sys.path
['', '/usr/local/lib/python24.zip', '/usr/local/lib/python2.4', '/usr/local/lib/python2.4/plat-freebsd4', '/usr/local/lib/python2.4/lib-tk', '/usr/local/lib/python2.4/lib-dynload', '/usr/local/lib/python2.4/site-packages']
大家以后寫好的模塊就可以放到上面的某一個(gè)目錄下,便可以正確搜索到了。當(dāng)然大家也可以添加自己的模塊路徑。Sys.path.append(“mine module path”).
(5)sys.modules
This is a dictionary that maps module names to modules which have already been loaded. This can be manipulated to force reloading of modules and other tricks.
Python.org手冊里已經(jīng)說的很明白了。
For names in sys.modules.keys():
If names != ’sys’:
……
(6)sys.stdin,sys.stdout,sys.stderr
stdin , stdout , 以及stderr 變量包含與標(biāo)準(zhǔn)I/O 流對應(yīng)的流對象. 如果需要更好地控制輸出,而print 不能滿足你的要求, 它們就是你所需要的. 你也可以替換它們, 這時(shí)候你就可以重定向輸出和輸入到其它設(shè)備( device ), 或者以非標(biāo)準(zhǔn)的方式處理它們
從網(wǎng)上摘抄的文章,供大家參考:
#testing stdout
print 'Hello World!'
運(yùn)行hello.py就會(huì)在標(biāo)準(zhǔn)輸出的屏幕上打印 Hello World!, 我們再編一個(gè)簡單的標(biāo)準(zhǔn)輸入的小程序 sayhi.py:
#testing stdin
print 'Hi, %s!' % raw_input('Please enter your name:')
當(dāng)你用鍵盤輸入你的名字后,程序在屏幕上輸出Hi,[你的名字]!, 這就是從標(biāo)準(zhǔn)輸入:鍵盤獲取信息,再輸出到標(biāo)準(zhǔn)輸出:屏幕的例子。
那么上面的例子中print 和 raw_input是如何與標(biāo)準(zhǔn)輸入/輸出流建立關(guān)系的呢?
其實(shí)Python程序的標(biāo)準(zhǔn)輸入/輸出/出錯(cuò)流定義在sys模塊中,分別 為: sys.stdin, sys.stdout, sys.stderr
上面的程序分別與下列的程序是一樣的:
import sys
sys.stdout.write('Hello World!')
import sys
print 'Please enter your name:',
name=sys.stdin.readline()[:-1]
print 'Hi, %s!' % name
那么sys.stdin, sys.stdout, stderr到底是什么呢?我們在Python運(yùn)行環(huán)境中輸入以下代碼:
import sys
for f in (sys.stdin, sys.stdout, sys.stderr): print f
輸出為:
', mode 'r' at 892210>
', mode 'w' at 892270>
', mode 'w at 8922d0>
由此可以看出stdin, stdout, stderr在Python中無非都是文件屬性的對象,他們在Python啟動(dòng)時(shí)自動(dòng)與Shell 環(huán)境中的標(biāo)準(zhǔn)輸入,輸出,出錯(cuò)關(guān)聯(lián)。
而Python程序的在Shell中的I/O重定向與本文開始時(shí)舉的DOS命令的重定向完全相同,其實(shí)這種重定向是由Shell來提供的,與Python 本身并無關(guān)系。那么我們是否可以在Python程序內(nèi)部將stdin,stdout,stderr讀寫操作重定向到一個(gè)內(nèi)部對象呢?答案是肯定的。
Python提供了一個(gè)StringIO模塊來完成這個(gè)設(shè)想,比如:
from StringIO import StringIO
import sys
buff =StringIO()
temp = sys.stdout ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #保存標(biāo)準(zhǔn)I/O流
sys.stdout = buff ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #將標(biāo)準(zhǔn)I/O流重定向到buff對象
print 42, 'hello', 0.001
sys.stdout =temp ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #恢復(fù)標(biāo)準(zhǔn)I/O流
print buff.getvalue()
總結(jié)
以上是生活随笔為你收集整理的python sys干嘛的_Python之sys模块的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: elisa标准曲线怎么做_ELISA标准
- 下一篇: python实现用户输入用户名和密码不能