Skip to content

Using Command Line Arguments in Python

Advertisement

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 the below content.

Then execute the above script with command line parameters.

python script.py first 2 third 4.5 

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

Output
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

Rahul

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top