Chapter 10 – Python Command Line Arguments

Step 1
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
#Author : Jijo K Jose
print "\n————————————\nChapter 10 – Command Line Parameters \n————————————\n"
import sys
if(len(sys.argv) < 3):
 print "You should enter 2 argument"
elif(len(sys.argv) >=3):
 print "You entered %d argument " %(len(sys.argv)-1)
 print "The sys.argv values are : ", sys.argv
 print "sys.argv values in separte lines are…."
 for i in sys.argv:
  print i
print "\n————————————–\n"

Step 2
Change file permission to executable using chmod command.
Step 3
Execute the python file.

You can leave a response, or trackback from your own site.

Leave a Reply