日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python sys干嘛的_Python之sys模块

發布時間:2023/12/2 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python sys干嘛的_Python之sys模块 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Sys模塊函數之多,我只能選取自己認為比較實用的一些函數列在此處。借馬云找員工的說法,”找最合適的而不是最天才的”,這句話,我個人覺得在很多方面都能適應,學習也不在話下。Sys模塊功能的確很多,但我們應該將重點放在那些功能才是最適合我們的,為此,我列的這些函數,就是我認為比較適合我以后開發的函數。

(1)sys.argv

很多人會想,我如何給我的程序在外部傳遞參數呢?這個,就可以實現。如:

Tesy.py

Import sys

Print sys.argv[number]

一般情況下,number為0是這個腳本的名字,1,2…則為命令行下傳遞的參數.如:

Test.py腳本內容:

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

看到,對應的關系了嗎?還有,在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

大家都知道,當今的程序比較流行的是跨平臺。簡單的說就是這段程序既可以在windows下,換到linux下也可以不加修改的運行起來,聽起來就不錯。所以,這個函數就可以派上用場了。

假設,我們想實現一個清除終端,linux下用clear, windows下用cls

Ostype=sys.platform()

If ostype==”linux” or ostype==”linux2”:

Cmd=”clear”

Else:

Cmd=”cls”

(3) sys.exit(n)

執行至主程序的末尾時,解釋器會自動退出. 但是如果需要中途退出程序, 你可以調用sys.exit 函數, 它帶有一個可選的整數參數返回給調用它的程序. 這意味著你可以在主程序中捕獲對sys.exit 的調用。(注: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?"

輸出結果:

[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程序中退出,將會產生一個systemExit異常,可以為此做些清除除理的工作。這個可選參數默認正常退出狀態是0,以數值為參數的范圍為:0-127。其他的數值為非正常退出,還有另一種類型,在這里展現的是strings對象類型。

(4)sys.path

大家對模塊都有一定了解吧?大家在使用模塊的某一個功能前,是不是需要導入呢?答案是需要。那import,__import__命令就不用提干嘛的了吧。那大家在執行import module_name的時候,python內部發生了什么呢?簡單的說,就是搜索module_name。根據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']

大家以后寫好的模塊就可以放到上面的某一個目錄下,便可以正確搜索到了。當然大家也可以添加自己的模塊路徑。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手冊里已經說的很明白了。

For names in sys.modules.keys():

If names != ’sys’:

……

(6)sys.stdin,sys.stdout,sys.stderr

stdin , stdout , 以及stderr 變量包含與標準I/O 流對應的流對象. 如果需要更好地控制輸出,而print 不能滿足你的要求, 它們就是你所需要的. 你也可以替換它們, 這時候你就可以重定向輸出和輸入到其它設備( device ), 或者以非標準的方式處理它們

從網上摘抄的文章,供大家參考:

#testing stdout

print 'Hello World!'

運行hello.py就會在標準輸出的屏幕上打印 Hello World!, 我們再編一個簡單的標準輸入的小程序 sayhi.py:

#testing stdin

print 'Hi, %s!' % raw_input('Please enter your name:')

當你用鍵盤輸入你的名字后,程序在屏幕上輸出Hi,[你的名字]!, 這就是從標準輸入:鍵盤獲取信息,再輸出到標準輸出:屏幕的例子。

那么上面的例子中print 和 raw_input是如何與標準輸入/輸出流建立關系的呢?

其實Python程序的標準輸入/輸出/出錯流定義在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運行環境中輸入以下代碼:

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啟動時自動與Shell 環境中的標準輸入,輸出,出錯關聯。

而Python程序的在Shell中的I/O重定向與本文開始時舉的DOS命令的重定向完全相同,其實這種重定向是由Shell來提供的,與Python 本身并無關系。那么我們是否可以在Python程序內部將stdin,stdout,stderr讀寫操作重定向到一個內部對象呢?答案是肯定的。

Python提供了一個StringIO模塊來完成這個設想,比如:

from StringIO import StringIO

import sys

buff =StringIO()

temp = sys.stdout ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #保存標準I/O流

sys.stdout = buff ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #將標準I/O流重定向到buff對象

print 42, 'hello', 0.001

sys.stdout =temp ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #恢復標準I/O流

print buff.getvalue()

總結

以上是生活随笔為你收集整理的python sys干嘛的_Python之sys模块的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。