Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»Python»How to Pass Command Line Arguments to Python Script

    How to Pass Command Line Arguments to Python Script

    RahulBy RahulAugust 19, 20191 Min ReadUpdated:September 19, 2019

    You can easily pass command line arguments to a Python script. In this tutorial, we will help you to read the command line arguments in a Python script.

    Below is the sample Python script, which reads the command line arguments and print details. Create a sample script like script.py and copy below content.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    #!/usr/bin/python
     
    import sys
     
    # Print total number of arguments
    print ('Total number of arguments:', format(len(sys.argv)))
     
    # Print all arguments
    print ('Argument List:', str(sys.argv))
     
    # Print arguments one by one
    print ('First argument:',  str(sys.argv[0]))
    print ('Second argument:',  str(sys.argv[1]))
    print ('Third argument:',  str(sys.argv[2]))
    print ('Fourth argument:',  str(sys.argv[3]))

    Then execute the above script with command line parameters.

    python script.py first 2 third 4.5
    

    You will see the results like below. The first argument is always the script itself.

    Total number of arguments: 5
    Argument List: ['script.py', 'first', '2', 'third', '4.5']
    First argument: script.py
    Second argument: first
    Third argument: 2
    Fourth argument: third
    
    Python scripts
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleAuto Renew Let’s Encrypt Certificates using Certbot
    Next Article How to Install and Use virtualenv with Python 3

    Related Posts

    Setup Selenium with Python and Chrome on Ubuntu & Debian

    Updated:June 20, 20224 Mins Read

    Setup Selenium with Python and Chrome on Fedora

    Updated:June 18, 20223 Mins Read

    Creating Python Virtual Environment on Windows

    Updated:June 3, 20222 Mins Read

    How to Find Django Install Location in Linux

    Updated:April 27, 20221 Min Read

    How To Install Python 3.10 on Debian 11/10

    2 Mins Read

    How to Install Python 3.10 on CentOS/RHEL 8 & Fedora 36/35

    Updated:June 6, 20223 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    • How to Install Angular CLI on Ubuntu 22.04
    • How to Install Composer on Ubuntu 22.04
    • How to Create DMARC Record For Your Domain
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.