Command Line Arguments in Python

Command Line Arguments in Python

Up to now, we have seen input in python using input () function. There is another method that uses command line arguments. The command line arguments must be given whenever we want to give the input before the start of the program, while on the other hand, input() is used to get the input while the python program is running.

अब तक, हमने इनपुट() फंक्शन का उपयोग करके पायथन में इनपुट देखा है। एक अन्य विधि है जो कमांड लाइन तर्कों का उपयोग करती है। जब भी हम प्रोग्राम शुरू होने से पहले इनपुट देना चाहते हैं, तो कमांड लाइन की तर्क दी जानी चाहिए, जबकि दूसरी तरफ इनपुट() का उपयोग इनपुट प्राप्त करने के लिए किया जाता है, जबकि पायथन प्रोग्राम चल रहा है।

Finally, command line arguments are another way to read user provided input. The arguments which are passing from the command prompt at the time of executing python script is nothing but command line arguments.

अंत में, कमांड लाइन तर्क उपयोगकर्ता द्वारा प्रदान किए गए इनपुट को रीड करने का एक और तरीका है। पायथन स्क्रिप्ट को निष्पादित करने के समय कमांड प्रॉम्प्ट से जो तर्क गुजर रहे हैं, वह कमांड लाइन के तर्कों के अलावा और कुछ नहीं है।

>>> py demo1.py 10 20 30
Here ‘py’ is the command
demo.py’ is name of the program
10, 20 and 30 are the command line arguments

Then how to read these command line arguments inside ‘demo.py’?

फिर इन कमांड लाईन के तर्कों को ‘demo.py’ के अंदर कैसे रीड करें?

Let Us see here: The command line arguments in python can be processed by using either ‘sys’ module. ‘argv’ is a list type variable present inside the ‘sys’ module. Through this variable we can happily read the command line arguments.

उत्तर: पायथन में कमांड लाइन के तर्क को ‘sys’ मॉड्यूल का उपयोग करके प्रोसेस किया जा सकता है। ‘argv’ एक सूची टाईप वैरियेबल है जो ‘sys’ मॉड्यूल के अंदर मौजूद है। इस वैरियेबल के माध्यम से हम कमांड लाइन तर्कों को खुशी से रीड कर सकते हैं।

Example: Check type of ‘argv’ from sys.

उदाहरण: sys से ‘argv’ के प्रकार की जाँच करें।
from sys import argv
print (type (argv))

Output:
<class ‘list’>

Example: Write a program to display number of command line arguments, length of command line arguments.

कमांड लाइन तर्क की संख्या, कमांड लाइन तर्क प्रदर्शित करने के लिए एक प्रोग्राम लिखें।

from sys import argv 
print ("The no.of command line arguments : ", len (argv) ) 
print ("The list of command line arguments : ", argv) 
print ("Command line arguments are ... ")
for i in argv:
    print (i) 

Output:
C:\python38\demo1.py 10 20 30
10
20
30

Example 2: To print the sum of command line arguments.
उदाहरण 2: कमांड लाइन तर्कों का योग प्रिंट करने के लिए।

from sys import argv 
sum = 0
args = argv[1:] 
for i in args:
    sum = sum + int (i) 
print (sum)

Output:
C:\python38\demo1.py 10 20 30 40
100

Note: Usually space is the separator between command line arguments. If our command line argument itself contains space then we should enclose within double quotes (but not single quotes).

नोट: आमतौर पर स्पेस कमांड लाइन के तर्कों के बीच सेपरेटर है। यदि हमारी कमांड लाइन तर्क में ही जगह है तो हमें दोहरें कोट्स चिन्हों के भीतर संलग्न होना चाहिए (लेकिन एकल कोट्स नहीं)

Example:
from sys import argv
print (argv[1])

Output:
C:\python38\demo1.py gyancs
C:\python38\demo1.py ‘gyancs school’ ‘gyancs
C:\python38\demo1.py “gyancs school” gyancs school

Note: Within the Python program command line arguments are available in the string from. Based on our requirement, we can convert into corresponding type by using type casting methods.

नोट: पायथन प्रोग्राम के भीतर कमांड लाइन के तर्क स्ट्रिंग में उपलब्ध हैं। हमारी आवश्यकता के आधार पर, हम टाइप कास्टिंग विधियों का उपयोग करके संगत प्रकार में परिवर्तित कर सकते हैं।

Example:
from sys import argv
print (argv [1] +argv[2])
print(int (argv[1])+int (argv[2]))

Output:
C:\python38\demo1.py 100 200
100200
300

Note: If we are trying to access command line arguments with ‘out of range index’ then we will get an error message.

नोट: यदि हम तर्क ‘आउट ऑफ रेंज इंडेक्स’ के साथ कमांड लाइन के तर्कों को एक्सेस करने की कोशिश कर रहे हैं, तो हमें एक एरर मैसेज मिलेगा।

Example:
from sys import argv
print (argv[10])

Output:
C:\python38\demo1.py 100
IndexError: list index out of range

Chapter wise Model Paper Link

About Me