Skip to content
Related Articles

Related Articles

Pass list as command line argument in Python

View Discussion
Improve Article
Save Article
  • Last Updated : 30 Jan, 2020
View Discussion
Improve Article
Save Article

The arguments that are given after the name of the program in the command line shell of the operating system are known as Command Line Arguments. Python provides various ways of dealing with these types of arguments. One of them is sys module.

sys Module

A module is a file containing Python definitions and statements. The sys module provides functions and variables used to manipulate different parts of the Python runtime environment. This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter.

sys.argv

sys.argv is used in python to retrieve command line arguments at runtime. For a program to be able to use it, it has to be imported from the “sys” module. The arguments so obtained are in the form of an array named sys.argv.

Note: sys.argv[0] gives the name of the program and the following indices work like members of an array.

Approach:
The name of the program is “cmdlist.py”.

  1. Working with command line: Consider the below code written in cmdlis.py




    import sys
      
      
    print("the name of the program is ", sys.argv[0])
    print("argument list :", sys.argv)

    Output:

    python-command-line

  2. Calling a list using the command line




    import sys
      
      
    print ("the name of the program is ", sys.argv[0])
      
    a = sys.argv[1]
    print (a)

    Output:

    python-command-line

  3. Working with the list called by command line.
    sys.argv is a string array. Thus, any argument passed to it is a string so in order to properly put in use we have to convert it to an appropriate list form.

    • Program to take list as command line argument and convert it to a proper list form




      import sys
        
        
      print("the name of the program is ", sys.argv[0])
        
      n = len(sys.argv[1])
      a = sys.argv[1][1:n-1]
      a = a.split(', ')
        
      for i in a:
          print(i)

      Output:
      python-command-line-argument

    • Program to find the sum of all the members of the list
      Also, keep in find to change the members to the required data type if not string.




      import sys
        
        
      print ("the name of the program is", sys.argv[0])
        
      n=len(sys.argv[1])
      a=sys.argv[1][1:n-1]
      a=a.split(',')
        
      A = [int(i) for i in a]
        
      b = 0
        
      for i in A:
          b += i
      print("sum of all the list members is ", b)

      Output:

      python-command-line

  4. Working with multiple list retrieved through command line.




    import sys
      
      
    print ("the name of the program is ", sys.argv[0])
      
    n = len(sys.argv[1])
    a = sys.argv[1][1:n-1]
    a = a.split(', ')
      
    A = [int(i) for i in a]
    b = 0
      
    for i in A:
        b += i
    print("sum of all the list members is ", b)
      
    n = len(sys.argv[2])
    c = sys.argv[2][1:n-1]
    c = c.split(', ')
      
    d = ""
      
    for i in c:
        d = d+i
      
    print ("sum of all the list members is ", d)

    Output:
    python-command-line


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!