久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

通過代碼實(shí)例了解Python sys模塊

瀏覽:108日期:2022-07-11 11:07:52

sys-系統(tǒng)特定的參數(shù)和功能

該模塊提供對解釋器使用或維護(hù)的一些變量的訪問,以及與解釋器強(qiáng)烈交互的函數(shù)。它始終可用。

代碼如下

#!/usr/bin/python# Filename: cat.pyimport sysdef 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 hereif 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)

這個程序用來模仿linux中的cat命令。

在python程序運(yùn)行的時候,即不是在交互模式下,在sys.argv[]列表中總是至少有一個項目,它就是當(dāng)前運(yùn)行的程序的名稱,其他的命令行參數(shù)在這個項目之后。

另外,sys模塊中還有其他特別有用的項目,sys.stdin sys.stdout sys.stderr分別對應(yīng)標(biāo)準(zhǔn)輸入、標(biāo)準(zhǔn)輸出、標(biāo)準(zhǔn)錯誤。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 濉溪县| 和平县| 南投县| 都江堰市| 湖州市| 利川市| 鄂托克前旗| 南开区| 陕西省| 武功县| 卢氏县| 镇宁| 宾阳县| 牙克石市| 全州县| 沙雅县| 芜湖市| 德州市| 聂拉木县| 固镇县| 安福县| 齐河县| 湘阴县| 秦安县| 合肥市| 普安县| 如东县| 金坛市| 洛宁县| 兰西县| 博客| 城固县| 大英县| 青岛市| 同心县| 双峰县| 宁晋县| 钦州市| 灵丘县| 阳西县| 南丹县|