How do I access command line arguments in Python?

Python Methods and Functions

Command line &this is where executable commands are passed to the operating system. A Python script can be executed by writing its name before the python executable on the command line.

 C: usersacer & gt; python test.py 

If you want some items of data to be passed to the Python script for processing, these items are written as space-separated values, followed by the script name. This space-separated list of values ​​is called command line arguments. 


Example

 C: usersacer & gt; python test.py Hello TutorialsPoint 

Space-separated items are stored in a special a List object called argv []. This is defined in the sys module of the Python distribution.





In the above example, the List object will contain:

 sys.argv [] = ['test.py', 'Hello', 'TutorialsPoint'] 

Program access to these arguments

 import sys print (" first command line argument: ", sys.argv [1]) print (" second command line argument: ", sys.argv [2]) 







Get Solution for free from DataCamp guru