Skip to content

Using Command Line Arguments in Python

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.

Category
Python
Versions & support
About Python →

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

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

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


Back to top