How do I access command line arguments?

408,478

Solution 1

Python tutorial explains it:

import sys

print(sys.argv)

More specifically, if you run python example.py one two three:

>>> import sys
>>> print(sys.argv)
['example.py', 'one', 'two', 'three']

Solution 2

To get only the command line arguments

(not including the name of the Python file)

import sys

sys.argv[1:]

The [1:] is a slice starting from the second element (index 1) and going to the end of the arguments list. This is because the first element is the name of the Python file, and we want to remove that.

Solution 3

I highly recommend argparse which comes with Python 2.7 and later.

The argparse module reduces boiler plate code and makes your code more robust, because the module handles all standard use cases (including subcommands), generates the help and usage for you, checks and sanitize the user input - all stuff you have to worry about when you are using sys.argv approach. And it is for free (built-in).

Here a small example:

import argparse

parser = argparse.ArgumentParser("simple_example")
parser.add_argument("counter", help="An integer will be increased by 1 and printed.", type=int)
args = parser.parse_args()
print(args.counter + 1)

and the output for python prog.py -h

usage: simple_example [-h] counter
    
positional arguments:
  counter     counter will be increased by 1 and printed.
    
optional arguments:
  -h, --help  show this help message and exit

and the output for python prog.py 1 As one would expect:

2

Solution 4

Python code:

import sys

# main
param_1= sys.argv[1] 
param_2= sys.argv[2] 
param_3= sys.argv[3]  
print 'Params=', param_1, param_2, param_3

Invocation:

$python myfile.py var1 var2 var3

Output:

Params= var1 var2 var3 

Solution 5

You can use sys.argv to get the arguments as a list.

If you need to access individual elements, you can use

sys.argv[i]  

where i is index, 0 will give you the python filename being executed. Any index after that are the arguments passed.

Share:
408,478
ParisNakitaKejser
Author by

ParisNakitaKejser

I'm a funny guy you probably want to defined as a geek/nerd, i'm from Denmark a small country on 5-6 million people and we speaking the Danish language. I'm studen a lot of stuff about computer, software developer and database technologies from books, internet and other blogs, if i need help i go to stackoverflow.com. And if you like to watch tutorials online, i have in 2015 open my owen YouTube Channel where i can share my know how to orther if you are intresset you can follow me there, every thing i publish its for free i just love to share what i know.

Updated on July 18, 2022

Comments

  • ParisNakitaKejser
    ParisNakitaKejser almost 2 years

    I use python to create my project settings setup, but I need help getting the command line arguments.

    I tried this on the terminal:

    $python myfile.py var1 var2 var3
    

    In my Python file, I want to use all variables that are input.

  • Kellen Stuart
    Kellen Stuart almost 6 years
    What does [1:] mean?
  • Kamil Jarosz
    Kamil Jarosz almost 6 years
    @KolobCanyon it means "take a sublist starting from index 1 till the end", i.e. skip the first element
  • Bassinator
    Bassinator over 5 years
    Quality answer, not really sure why anyone here would use any of the other approaches given here. Especially since it's built in! Don't you guys have better things to do than parsing?
  • look
    look about 5 years
    sys.argv[0] is not the current working directory. It is the name of the file that is executing.
  • Samuel Nde
    Samuel Nde almost 5 years
    @KolobCanyon take the the second argument [1:] upwards because the first argument of sys.argv is by default the name of the python file that you are running for example testrun.py.
  • MikeL
    MikeL almost 5 years
    Just to mention how you should access the specific argument in args namespace: args.counter
  • reducing activity
    reducing activity over 4 years
    @Bassinator In cases when no parsing of arguments is needed.
  • Albicocco
    Albicocco about 4 years
    to use default value, without specific value in the command line, you can use something like: parser.add_argument('var3', nargs='?', type=int, default=3) var3 is a optional param, if you don't specify a value in the command line you can access to var3 in your code, default value for var3=3
  • alper
    alper over 3 years
    Can type be int or float?
  • Michael Dorner
    Michael Dorner over 3 years
  • Jason Harrison
    Jason Harrison about 3 years
    @Bassinator sometimes you want to record the exact command used. For example in machine learning experiments, logging sys.argv is an easy way to log the command line without having to update the code after any improvements to the argument definitions for argparse.ArgumentParser
  • Josep Escobar
    Josep Escobar about 2 years
    You are the boss! Thanks man!
  • Michael Dorner
    Michael Dorner about 2 years
    I feel flattered. :) Thanks man! :)