最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python中sys模塊的介紹與實(shí)例

 更新時(shí)間:2021年04月17日 08:48:06   作者:zyc_love_study  
這篇文章主要給大家介紹了關(guān)于python中sys模塊的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

python版本: Python 2.7.6

1: sys是python自帶模塊.

利用 import 語(yǔ)句輸入sys 模塊。

當(dāng)執(zhí)行import sys后, python在 sys.path 變量中所列目錄中尋找 sys 模塊文件。然后運(yùn)行這個(gè)模塊的主塊中的語(yǔ)句進(jìn)行初始化,然后就可以使用模塊了 。

2: sys模塊常見(jiàn)函數(shù)

可以通過(guò)dir()方法查看模塊中可用的方法. 結(jié)果如下, 很多我都沒(méi)有用過(guò), 所以只是簡(jiǎn)單介紹幾個(gè)自己用過(guò)的方法.

$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', '_mercurial', '_multiarch', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'hexversion', 'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'py3kwarning', 'pydebug', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions']
1

(1) sys.argv 實(shí)現(xiàn)從程序外部向程序傳遞參數(shù)

sys.argv 變量是一個(gè)包含了命令行參數(shù)的字符串列表, 利用命令行想程序傳遞參數(shù). 其中,腳本的名稱(chēng)總是 sys.argv 列表的第一個(gè)參數(shù)。

(2) sys.path 包含輸入模塊的目錄名列表。

獲取指定模塊搜索路徑的字符串集合,可以將寫(xiě)好的模塊放在得到的某個(gè)路徑下,就可以在程序中import時(shí)正確找到。在import導(dǎo)入module_name時(shí),就是根據(jù)sys.path的路徑來(lái)搜索module.name,也可以自定義添加模塊路徑。
sys.path.append(“自定義模塊路徑”)

(3) sys.exit([arg]) 程序中間的退出, arg=0為正常退出

一般情況下執(zhí)行到主程序末尾,解釋器自動(dòng)退出,但是如果需要中途退出程序,可以調(diào)用sys.exit函數(shù),帶有一個(gè)可選的整數(shù)參數(shù)返回給調(diào)用它的程序,表示你可以在主程序中捕獲對(duì)sys.exit的調(diào)用。(0是正常退出,其他為異常)當(dāng)然也可以用字符串參數(shù),表示錯(cuò)誤不成功的報(bào)錯(cuò)信息。

(4) sys.modules

sys.modules是一個(gè)全局字典,該字典是python啟動(dòng)后就加載在內(nèi)存中。每當(dāng)程序員導(dǎo)入新的模塊,sys.modules將自動(dòng)記錄該模塊。當(dāng)?shù)诙卧賹?dǎo)入該模塊時(shí),python會(huì)直接到字典中查找,從而加快了程序運(yùn)行的速度。它擁有字典所擁有的一切方法.

(5) sys.getdefaultencoding() / sys.setdefaultencoding() / sys.getfilesystemencoding()

sys.getdefaultencoding()

獲取系統(tǒng)當(dāng)前編碼,一般默認(rèn)為ascii。

sys.setdefaultencoding()

設(shè)置系統(tǒng)默認(rèn)編碼,執(zhí)行dir(sys)時(shí)不會(huì)看到這個(gè)方法,在解釋器中執(zhí)行不通過(guò),可以先執(zhí)行reload(sys),在執(zhí)行 setdefaultencoding(‘utf8'),此時(shí)將系統(tǒng)默認(rèn)編碼設(shè)置為utf8。(見(jiàn)設(shè)置系統(tǒng)默認(rèn)編碼 )

sys.getfilesystemencoding()

獲取文件系統(tǒng)使用編碼方式,Windows下返回'mbcs',mac下返回'utf-8'

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

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

(7) sys.platform

獲取當(dāng)前系統(tǒng)平臺(tái). 如:win32、Linux等。

3: 實(shí)例

(1) sys.argv sys.path

$ cat sys-test.py
 #!/usr/bin/python
import sys

print 'The command line arguments are:'
for i in sys.argv:
    print i

print '\n\nThe PYTHONPATH is', sys.path, '\n'

運(yùn)行結(jié)果:

$ python sys-test.py my name is ubuntu
The command line arguments are:
sys-test.py
my
name
is
ubuntu

The PYTHONPATH is ['/work/python-practice', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

(2) sys.exit

 #!/usr/bin/python
import sys

def exitfunc(value):
    print (value)
    sys.exit(0)

print("hello")

try:
    sys.exit(90)
except SystemExit as value:
    exitfunc(value)   

print("come?")

運(yùn)行結(jié)果:

hello
90

程序首先打印hello,在執(zhí)行exit(90),拋異常把90傳給values,values在傳進(jìn)函數(shù)中執(zhí)行,打印90程序退出。后面的”come?”因?yàn)橐呀?jīng)退出所以不會(huì)被打印. 而此時(shí)如果把exitfunc函數(shù)里面的sys.exit(0)去掉,那么程序會(huì)繼續(xù)執(zhí)行到輸出”come?”.

(3) sys.modules

sys.modules.keys() 返回所有已經(jīng)導(dǎo)入的模塊列表

keys是模塊名

values是模塊

modules返回路徑

 #!/usr/bin/python
import sys

print(sys.modules.keys())
print("**************************************************************************")
print(sys.modules.values())
print("**************************************************************************")
print(sys.modules["os"])

運(yùn)行結(jié)果:

['copy_reg', 'sre_compile', '_sre', 'encodings', 'site', '__builtin__', 'sysconfig', '__main__', 'encodings.encodings', 'abc', 'posixpath', '_weakrefset', 'errno', 'encodings.codecs', 'sre_constants', 're', '_abcoll', 'types', '_codecs', 'encodings.__builtin__', '_warnings', 'genericpath', 'stat', 'zipimport', '_sysconfigdata', 'warnings', 'UserDict', 'encodings.ascii', 'sys', 'codecs', '_sysconfigdata_nd', 'os.path', 'sitecustomize', 'signal', 'traceback', 'linecache', 'posix', 'encodings.aliases', 'exceptions', 'sre_parse', 'os', '_weakref']
*******************************************************************************
[<module 'copy_reg' from '/usr/lib/python2.7/copy_reg.pyc'>, <module 'sre_compile' from '/usr/lib/python2.7/sre_compile.pyc'>, <module '_sre' (built-in)>, <module 'encodings' from '/usr/lib/python2.7/encodings/__init__.pyc'>, <module 'site' from '/usr/lib/python2.7/site.pyc'>, <module '__builtin__' (built-in)>, <module 'sysconfig' from '/usr/lib/python2.7/sysconfig.pyc'>, <module '__main__' from 'sys-test1.py'>, None, <module 'abc' from '/usr/lib/python2.7/abc.pyc'>, <module 'posixpath' from '/usr/lib/python2.7/posixpath.pyc'>, <module '_weakrefset' from '/usr/lib/python2.7/_weakrefset.pyc'>, <module 'errno' (built-in)>, None, <module 'sre_constants' from '/usr/lib/python2.7/sre_constants.pyc'>, <module 're' from '/usr/lib/python2.7/re.pyc'>, <module '_abcoll' from '/usr/lib/python2.7/_abcoll.pyc'>, <module 'types' from '/usr/lib/python2.7/types.pyc'>, <module '_codecs' (built-in)>, None, <module '_warnings' (built-in)>, <module 'genericpath' from '/usr/lib/python2.7/genericpath.pyc'>, <module 'stat' from '/usr/lib/python2.7/stat.pyc'>, <module 'zipimport' (built-in)>, <module '_sysconfigdata' from '/usr/lib/python2.7/_sysconfigdata.pyc'>, <module 'warnings' from '/usr/lib/python2.7/warnings.pyc'>, <module 'UserDict' from '/usr/lib/python2.7/UserDict.pyc'>, <module 'encodings.ascii' from '/usr/lib/python2.7/encodings/ascii.pyc'>, <module 'sys' (built-in)>, <module 'codecs' from '/usr/lib/python2.7/codecs.pyc'>, <module '_sysconfigdata_nd' from '/usr/lib/python2.7/plat-x86_64-linux-gnu/_sysconfigdata_nd.pyc'>, <module 'posixpath' from '/usr/lib/python2.7/posixpath.pyc'>, <module 'sitecustomize' from '/usr/lib/python2.7/sitecustomize.pyc'>, <module 'signal' (built-in)>, <module 'traceback' from '/usr/lib/python2.7/traceback.pyc'>, <module 'linecache' from '/usr/lib/python2.7/linecache.pyc'>, <module 'posix' (built-in)>, <module 'encodings.aliases' from '/usr/lib/python2.7/encodings/aliases.pyc'>, <module 'exceptions' (built-in)>, <module 'sre_parse' from '/usr/lib/python2.7/sre_parse.pyc'>, <module 'os' from '/usr/lib/python2.7/os.pyc'>, <module '_weakref' (built-in)>]
*******************************************************************************
<module 'os' from '/usr/lib/python2.7/os.pyc'>

(4) sys.stdin/sys.stdout/sys.stderr

stdin,stdout,stderr在Python中都是文件屬性對(duì)象, 他們?cè)趐ython啟動(dòng)時(shí)自動(dòng)與shell環(huán)境中的標(biāo)準(zhǔn)輸入, 輸出, 出錯(cuò)相關(guān). 而python程序在shell中的I/O重定向是有shell來(lái)提供的,與python本身沒(méi)有關(guān)系.python程序內(nèi)部將stdin, stdout, stderr讀寫(xiě)操作重定向到一個(gè)內(nèi)部對(duì)象.

標(biāo)準(zhǔn)輸入
#!/usr/bin/python
import sys
#print('Hi, %s!' %input('Please enter your name: ')) python3.*版本用input
print('Hi, %s!' %raw_input('Please enter your name: ')) #python2.*版本用raw_input
運(yùn)行結(jié)果:
Please enter your name: er
Hi, er!
等同于:
 #!/usr/bin/python
import sys
print('Please enter your name:')
name=sys.stdin.readline()[:-1]
print('Hi, %s!' %name)
標(biāo)準(zhǔn)輸出
print('Hello World!\n')
等同于:
 #!/usr/bin/python
import sys
sys.stdout.write('output resule is good!\n')
其他實(shí)驗(yàn)
#!/usr/bin/python
import sys

for i in (sys.stdin, sys.stdout, sys.stderr):
    print(i)

執(zhí)行結(jié)果:

python sys-test1.py
<open file '<stdin>', mode 'r' at 0x7fa4e630f0c0>
<open file '<stdout>', mode 'w' at 0x7fa4e630f150>
<open file '<stderr>', mode 'w' at 0x7fa4e630f1e0>

總結(jié)

到此這篇關(guān)于python中sys模塊的介紹與實(shí)例的文章就介紹到這了,更多相關(guān)python中sys模塊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python的特殊語(yǔ)法和常用模塊示例詳解

    python的特殊語(yǔ)法和常用模塊示例詳解

    Python確實(shí)支持函數(shù)式編程,并提供了一些內(nèi)置的高階函數(shù),這些函數(shù)可以接受其他函數(shù)作為參數(shù),從而使代碼更加簡(jiǎn)潔和功能強(qiáng)大,這篇文章主要介紹了python的特殊語(yǔ)法和常用模塊詳解,需要的朋友可以參考下
    2024-08-08
  • python logging.basicConfig不生效的原因及解決

    python logging.basicConfig不生效的原因及解決

    今天小編就為大家分享一篇python logging.basicConfig不生效的原因及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • django之導(dǎo)入并執(zhí)行自定義的函數(shù)模塊圖解

    django之導(dǎo)入并執(zhí)行自定義的函數(shù)模塊圖解

    這篇文章主要介紹了django之導(dǎo)入并執(zhí)行自定義的函數(shù)模塊圖解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • Python庫(kù)docopt命令行參數(shù)解析工具

    Python庫(kù)docopt命令行參數(shù)解析工具

    這篇文章主要介紹了Python庫(kù)docopt命令行參數(shù)解析工具,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • Python利用FlashText算法實(shí)現(xiàn)替換字符串

    Python利用FlashText算法實(shí)現(xiàn)替換字符串

    FlashText算法是由?Vikash?Singh?于2017年發(fā)表的大規(guī)模關(guān)鍵詞替換算法,比正則表達(dá)式替換快M倍以上,這個(gè)M是需要替換的關(guān)鍵詞數(shù)量,關(guān)鍵詞越多,F(xiàn)lashText算法的優(yōu)勢(shì)就越明顯。本文將詳細(xì)這一算法,需要的可以參考一下
    2022-03-03
  • python 如何獲取元素在array中的下標(biāo)

    python 如何獲取元素在array中的下標(biāo)

    這篇文章主要介紹了python 獲取元素在array中的下標(biāo)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • Python學(xué)習(xí)筆記之列表和成員運(yùn)算符及列表相關(guān)方法詳解

    Python學(xué)習(xí)筆記之列表和成員運(yùn)算符及列表相關(guān)方法詳解

    這篇文章主要介紹了Python學(xué)習(xí)筆記之列表和成員運(yùn)算符及列表相關(guān)方法,結(jié)合實(shí)例形式詳細(xì)分析了Python列表相關(guān)的概念、原理、成員函數(shù)與相關(guān)使用技巧,需要的朋友可以參考下
    2019-08-08
  • 如何用Python進(jìn)行時(shí)間序列分解和預(yù)測(cè)

    如何用Python進(jìn)行時(shí)間序列分解和預(yù)測(cè)

    這篇文章主要介紹了如何用Python進(jìn)行時(shí)間序列分解和預(yù)測(cè),幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-02-02
  • python實(shí)現(xiàn)aes加密及pycryptodome庫(kù)使用

    python實(shí)現(xiàn)aes加密及pycryptodome庫(kù)使用

    AES算法是高級(jí)加密標(biāo)準(zhǔn),它是一種對(duì)稱(chēng)加密算法,AES只有一個(gè)密鑰,這個(gè)密鑰既用來(lái)加密,也用于解密,這篇文章主要給大家介紹了關(guān)于python實(shí)現(xiàn)aes加密及pycryptodome庫(kù)使用的相關(guān)資料,需要的朋友可以參考下
    2023-10-10
  • 詳解Python如何巧妙實(shí)現(xiàn)數(shù)學(xué)階乘n!

    詳解Python如何巧妙實(shí)現(xiàn)數(shù)學(xué)階乘n!

    一個(gè)正整數(shù)的階乘(factorial)是所有小于及等于該數(shù)的正整數(shù)的積,并且0的階乘為1。自然數(shù)n的階乘寫(xiě)作n!,本文就給大家介紹如何使用python和第三方庫(kù)來(lái)實(shí)現(xiàn)數(shù)學(xué)運(yùn)算中的階乘以及階乘累計(jì)求和
    2023-03-03

最新評(píng)論

江阴市| 凯里市| 涞源县| 资阳市| 边坝县| 通海县| 靖远县| 介休市| 凤山市| 丹巴县| 海城市| 邵东县| 崇仁县| 左贡县| 广东省| 永登县| 正定县| 堆龙德庆县| 黄冈市| 桐柏县| 南溪县| 黄浦区| 靖边县| 洪泽县| 高密市| 南平市| 绥江县| 彭山县| 通城县| 同德县| 三河市| 山东省| 乐陵市| 平顶山市| 马关县| 东乡| 穆棱市| 阳东县| 保山市| 辰溪县| 佛学|