What does "sys.argv[1]" mean? (What is sys.argv, and where does it come from?) Ask Question

What does

I'm currently teaching myself Python and was just wondering (In reference to my example below) in simplified terms what the sys.argv[1] represents. Is it simply asking for an input?


#!/usr/bin/python3.1

# import modules used here -- sys is a very standard one
import sys

# Gather our code in a main() function
def main():
  print ('Hello there', sys.argv[1])
  # Command line args are in sys.argv[1], sys.argv[2] ..
  # sys.argv[0] is the script name itself and can be ignored

# Standard boilerplate to call the main() function to begin
# the program.
if __name__ == '__main__':
  main()

You may have been directed here because you were asking about an IndexError in your code that uses sys.argv. The problem is not in your code; the problem is that you need to run the program in a way that makes sys.argv contain the right values. Please read the answers to understand how sys.argv works.

If you have read and understood the answers, and are still having problems on Windows, check if Python Script does not take sys.argv in Windows fixes the issue. If you are trying to run the program IDE内からIDE 固有のヘルプが必要になる場合があります。検索してください。ただし、最初にコマンド ラインからプログラムを正常に実行できるかどうかを確認してください。

ベストアンサー1

以前の回答では、ユーザーの知識について多くの仮定が立てられていたことに注意してください。この回答では、よりチュートリアルレベルで質問に答えようとしています。

Pythonの呼び出しごとに、sys.argvコマンドライン上の引数(スペースで区切られた)を表す文字列のリストが自動的に作成されます。名前はCプログラミング規約ここで、argv と argc はコマンドライン引数を表します。

Python に慣れていくにつれて、リストと文字列についてさらに詳しく学びたくなるでしょうが、それまでの間、知っておくべきことがいくつかあります。

引数をそのまま出力するスクリプトを簡単に作成できます。また、lenリストの関数を使用して引数の数も出力します。

from __future__ import print_function
import sys
print(sys.argv, len(sys.argv))

このスクリプトには Python 2.6 以降が必要です。このスクリプトを呼び出す場合print_args.py、異なる引数を指定して呼び出して、何が起こるかを確認できます。

> python print_args.py
['print_args.py'] 1

> python print_args.py foo and bar
['print_args.py', 'foo', 'and', 'bar'] 4

> python print_args.py "foo and bar"
['print_args.py', 'foo and bar'] 2

> python print_args.py "foo and bar" and baz
['print_args.py', 'foo and bar', 'and', 'baz'] 4

ご覧のとおり、コマンドライン引数にはスクリプト名が含まれていますが、インタプリタ名は含まれていません。この意味で、Pythonはスクリプトをとして実行可能ファイルの名前 (この場合は python) を知る必要がある場合は、 を使用できますsys.executable

例からわかるように、ユーザーが引用符で囲まれた引数を使用してスクリプトを呼び出すと、スペースを含む引数を受け取る可能性があり、取得されるのはユーザーが指定した引数のリストです。

これで、Python コードで、この文字列のリストをプログラムへの入力として使用できます。リストは 0 から始まる整数でインデックス付けされるため、list[0] 構文を使用して個々の項目を取得できます。たとえば、スクリプト名を取得するには、次のようにします。

script_name = sys.argv[0] # this will always work.

興味深いことですが、スクリプト名を知る必要はほとんどありません。ファイル名のスクリプトの後の最初の引数を取得するには、次のようにします。

filename = sys.argv[1]

これは非常に一般的な使用法ですが、引数が指定されていない場合は IndexError で失敗することに注意してください。

また、Pythonではリストのスライスを参照することができるので、別のリストユーザーが指定した引数(スクリプト名なし)のみの場合は、次のようにします。

user_args = sys.argv[1:] # get everything after the script name

さらに、Python では、変数名に項目のシーケンス (リストを含む) を割り当てることができます。したがって、ユーザーが常に 2 つの引数を指定することが予想される場合は、それらの引数 (文字列として) を 2 つの変数に割り当てることができます。

user_args = sys.argv[1:]
fun, games = user_args # len(user_args) had better be 2

したがって、あなたの特定の質問に答えると、は問題のスクリプトに提供されるsys.argv[1]最初のコマンドライン引数 ( としてstring) を表します。入力を求めるプロンプトは表示されませんが、スクリプト名の後にコマンドラインで引数が指定されていない場合は IndexError で失敗します。

おすすめ記事