k0b0's record.

Computer Engineering, Arts and Books

Python入門 コマンドライン引数

コマンドライン引数

 pythonのコマンドライン引数を使ってみる。

コマンドライン引数を使用した記述例(argument.py)

import sys

args = sys.argv

# Print argument list
print(args)

# Print argument.
print("argument 0 : " + args[0])
print("argument 1 : " + args[1])
print("argument 2 : " + args[2])

実行

$ python argument.py hello python
プログラム実行時に指定した引数
第0引数 argument.py(ファイル名)
第1引数 hello
第2引数 python

実行結果

['argument.py', 'hello', 'python']
argument 0 : argument.py
argument 1 : hello
argument 2 : python