LoginSignup
0
0

More than 5 years have passed since last update.

Python2.7で実行時引数を受け取る

Posted at

Python2.7で実行時引数を受け取る

スクリプトの実行時に渡した引数で処理を変える際に利用しました

動作環境

OS: Windows7
言語: Python 2.7.10

やり方

import sysを行いsys.argvの配列内に格納される引数を取得します

サンプルコード

test.py
#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys

if __name__ == "__main__":
    count = 0
    for roop in sys.argv:
        print str(count) + '番目引数 : ' + str(roop)
        count += 1

実行例

python test.py aiu e o 1 23

実行結果

0番目引数 : test.py
1番目引数 : aiu
2番目引数 : e
3番目引数 : o
4番目引数 : 1
5番目引数 : 23

Qiita用_引数.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0