在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Python/ Python命令行參數(shù)
Python異常處理
Python循環(huán)
Python基本運(yùn)算符
Python網(wǎng)絡(luò)編程(Sockets)
Python可以開發(fā)哪些程序?
Python XML解析和處理
Python數(shù)字
Python函數(shù)
Python變量類型
Python os模塊方法
Python迭代器
Python安裝和環(huán)境配置
Python構(gòu)造函數(shù)
Python文件對(duì)象方法
Python日期和時(shí)間
Python的歷史
Python生成器
Python+MySQL數(shù)據(jù)庫操作(PyMySQL)
Python命令行參數(shù)
Python元組
Python發(fā)送郵件
Python列表
Python文件讀寫
Python教程
Python面向?qū)ο螅惡蛯?duì)象)
Python多線程編程
Python多重繼承
Python決策
Python是什么?
Python快速入門
Python繼承
Python字典
Python字符串
Python操作符重載
Python正則表達(dá)式
Python閉包
Python修飾器
Python功能特點(diǎn)
Python模塊

Python命令行參數(shù)

Python提供了一個(gè)getopt模塊,用于解析命令行選項(xiàng)和參數(shù)。

$ python test.py arg1 arg2 arg3

Python sys模塊通過sys.argv提供對(duì)任何命令行參數(shù)的訪問。主要有兩個(gè)參數(shù)變量 -

  • sys.argv是命令行參數(shù)的列表。
  • len(sys.argv)是命令行參數(shù)的數(shù)量。

這里sys.argv [0]是程序名稱,即腳本的名稱。比如在上面示例代碼中,sys.argv [0]的值就是 test.py。

示例

看看以下腳本command_line_arguments.py的代碼 -

#!/usr/bin/python3

import sys

print ('Number of arguments:', len(sys.argv), 'arguments.')
print ('Argument List:', str(sys.argv))

現(xiàn)在運(yùn)行上面的腳本,這將產(chǎn)生以下結(jié)果 -

F:\>python F:\worksp\python\command_line_arguments.py
Number of arguments: 1 arguments.
Argument List: ['F:\\worksp\\python\\command_line_arguments.py']

F:\>python F:\worksp\python\command_line_arguments.py arg1 arg2 arg3 arg4
Number of arguments: 5 arguments.
Argument List: ['F:\\worksp\\python\\command_line_arguments.py', 'arg1', 'arg2', 'arg3', 'arg4']

F:\>

注意 - 如上所述,第一個(gè)參數(shù)始終是腳本名稱,它也被計(jì)入?yún)?shù)的數(shù)量。

解析命令行參數(shù)

Python提供了一個(gè)getopt模塊,可用于解析命令行選項(xiàng)和參數(shù)。該模塊提供了兩個(gè)功能和異常,以啟用命令行參數(shù)解析。

getopt.getopt方法

此方法解析命令行選項(xiàng)和參數(shù)列表。以下是此方法的簡單語法 -

getopt.getopt(args, options, [long_options])

getopt.GetoptError異常

當(dāng)在參數(shù)列表中有一個(gè)無法識(shí)別的選項(xiàng),或者當(dāng)需要一個(gè)參數(shù)的選項(xiàng)不為任何參數(shù)時(shí),會(huì)引發(fā)這個(gè)異常。
異常的參數(shù)是一個(gè)字符串,指示錯(cuò)誤的原因。 屬性msgopt給出錯(cuò)誤消息和相關(guān)選項(xiàng)。

示例

假設(shè)想通過命令行傳遞兩個(gè)文件名,也想給出一個(gè)選項(xiàng)用來顯示腳本的用法。腳本的用法如下 -

usage: file.py -i <inputfile> -o <outputfile>

以下是command_line_usage.py的以下腳本 -

#!/usr/bin/python3

import sys, getopt

def main(argv):
   inputfile = ''
   outputfile = ''
   try:
      opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
   except getopt.GetoptError:
      print ('GetoptError, usage: command_line_usage.py -i <inputfile> -o <outputfile>')
      sys.exit(2)
   for opt, arg in opts:
      if opt == '-h':
         print ('usage: command_line_usage.py -i <inputfile> -o <outputfile>')
         sys.exit()
      elif opt in ("-i", "--ifile"):
         inputfile = arg
      elif opt in ("-o", "--ofile"):
         outputfile = arg
   print ('Input file is "', inputfile)
   print ('Output file is "', outputfile)

if __name__ == "__main__":
   main(sys.argv[1:])

現(xiàn)在,使用以下幾種方式運(yùn)行來腳本,輸出如下所示:

F:\worksp\python>python command_line_usage.py -h
usage: command_line_usage.py -i <inputfile> -o <outputfile>

F:\worksp\python>python command_line_usage.py -i inputfile.txt -o
GetoptError, usage: command_line_usage.py -i <inputfile> -o <outputfile>

F:\worksp\python>python command_line_usage.py -i inputfile.txt -o outputfile.txt
Input file is " inputfile.txt
Output file is " outputfile.txt

F:\worksp\python>