python getopt模組怎麼用?
大家現正在學習命令列參嗎?用沒用到過getopt呢?今天小編,就不跟大家聊很多的內容了,直接給大家介紹今天的文章主題,就是關於前文所提到的getopet,小夥伴們對此有沒有什麼瞭解呢?沒有了解的小夥伴也不必擔心,因為下面小編給大家濃縮了這個知識點的精華,看一遍,就可以快速上手了。
例項
假定我們建立這樣一個指令碼,可以透過命令列向指令碼檔案傳遞兩個檔名,同時我們透過另外一個選項檢視指令碼的使用。
指令碼使用方法如下:
usage: test.py -i <inputfile> -o <outputfile>
test.py 檔案程式碼如下所示:
import sys, getopt def main(argv): inputfile = '' outputfile = '' try: opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="]) except getopt.GetoptError: print 'test.py -i <inputfile> -o <outputfile>' sys.exit(2) for opt, arg in opts: if opt == '-h': print 'test.py -i <inputfile> -o <outputfile>' sys.exit() elif opt in ("-i", "--ifile"): inputfile = arg elif opt in ("-o", "--ofile"): outputfile = arg print '輸入的檔案為:', inputfile print '輸出的檔案為:', outputfile if __name__ == "__main__": main(sys.argv[1:])
執行以上程式碼,輸出結果為:
$ python test.py -h usage: test.py -i <inputfile> -o <outputfile> $ python test.py -i inputfile -o outputfile 輸入的檔案為: inputfile 輸出的檔案為: outputfile
好啦,如果在碰到以上模組的使用方法,一定要看下這篇文章內容哦~都是所有疑難問題的解決集錦呢~小夥伴們仔細再看幾遍吧,如果還想了解其他內容,就到教程中心瀏覽吧~
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/4550/viewspace-2832210/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- python找不到指定模組怎麼辦Python
- 怎麼用anaconda安裝模組?
- python api怎麼用PythonAPI
- getopt在Python中的使用Python
- python中的複製copy模組怎麼使用?Python
- getopt和getopt_long
- getopt與getopt_long
- python的int怎麼用Python
- python中geopy怎麼用Python
- Python集合要怎麼用Python
- Python怎麼安裝第三方模組Python
- python TestCase測試用例怎麼用Python
- 怎麼用python扒網頁?Python網頁
- Python 中strip()方法怎麼用?Python
- python3.6print怎麼用Python
- Python中模組是什麼?Python有哪些模組?Python
- [Python]OS模組應用Python
- python中Matplotlib是什麼?怎麼用?Python
- getopt for windowsWindows
- python安裝之後怎麼用Python
- 怎麼用Python生成隨機數Python隨機
- 怎麼用python django做網站?PythonDjango網站
- python3.5中cmp怎麼用Python
- python的upper函式怎麼用Python函式
- Python中randrange()函式怎麼用?Python函式
- 命令列執行python時找不到模組怎麼解決命令列Python
- linux 中getopt和getopt_long 用法解析Linux
- 怎麼區分外掛和模組
- python中的find函式怎麼用Python函式
- 怎麼用python中tuple相關方法?Python
- [Python小記] 裝飾器怎麼用 ?Python
- 怎樣安裝python的GPIO模組Python
- 什麼是模組?Python模組分為哪幾類?Python
- 你真的知道Python的字串怎麼用嗎?Python字串
- python用install失敗怎麼解決Python
- python爬蟲需要什麼模組Python爬蟲
- c++中getopt和getopt_long的使用方法C++
- Linux-getoptLinux