import sys
#print sys.argv[1]
def IndexSearchString():
This function search a string in a file through index and gives the result.
#fieldindex = int(sys.argv[1])-1
stringsrch = sys.argv[2]
#size = len(sys.argv)
file_name = open("passwd", "r")
#print "No of Argu ", size
'''
if sys.argv[1] == Null:
print "Please enter argument"
'''
if sys.argv[1].isdigit():
fieldindex = int(sys.argv[1])-1
else:
print "Enter integer in 1st Argument"
sys.exit(1)
fieldindex = int(sys.argv[1])-1
stringsrch = sys.argv[2]
if len(sys.argv) > 3 :
print "Enter two arguments only"
sys.exit(1)
for store_file in file_name:
temp = store_file.split(":")
search = temp[fieldindex]
#print search
if stringsrch in search:
print store_file
IndexSearchString()
'''
My Question is:
I am taking two argument in the command line. Now I want to handle when only one argument is given and also when no argument is given.
Help me out
Error:
Command Window Input: python SearchString.py 1
Traceback (most recent call last):
File "SearchString.py", line 46, in <module>
IndexSearchString()
File "SearchString.py", line 36, in IndexSearchString
stringsrch = sys.argv[2]
IndexError: list index out of range
'''
Asked
Active
Viewed 6,441 times
-5

Kimvais
- 38,306
- 16
- 108
- 142

user2865801
- 15
- 1
- 4
-
1Format your code properly – Oct 10 '13 at 06:58
-
1Python has clearly modules like "argparse" for doing commandline options handling _properly_ - at least much better than your code. – Oct 10 '13 at 06:59
-
when i take no argument i want that to be handled without try except – user2865801 Oct 10 '13 at 07:03
-
Search string is missing – user2753523 Oct 10 '13 at 07:03
-
i am not giving search string, and its is giving me error and i want that to be handled – user2865801 Oct 10 '13 at 07:04
-
i am an newbie in python – user2865801 Oct 10 '13 at 07:05
-
Just use [`docopt`](http://docopt.org/) – Kimvais Oct 10 '13 at 12:02
-
How are you calling this program. Ie what are typing on the command line? – Kevin Oct 10 '13 at 14:43
4 Answers
0
this
stringsrch = sys.argv[2]
should probably be this
stringsrch = sys.argv[1]
because no other place do you use
sys.argv[2]

mjz19910
- 244
- 1
- 13
-
-
because of line 6 and 20 you would have to give the program two arguments you only gave it one – mjz19910 Oct 10 '13 at 08:01
-
`if len(sys.argv) < 2 : print "Enter two arguments" sys.exit(1)` you should also add this before you do anything in your script – mjz19910 Oct 10 '13 at 08:06
0
in your Opinion: you should start with 0 in sys.argv

Josh Crozier
- 233,099
- 56
- 391
- 304

micheal.yxd
- 118
- 1
- 7
0
check this question out and see if it can help
python and sys.argv
add this after your "def..."
if len(sys.argv) < 3 :
print "Please enter two arguments"
sys.exit(1)
and change your other one
if len(sys.argv) > 3 :
print "Enter two arguments only"
sys.exit(1)
to
if len(sys.argv) > 4 :
print "Enter only two arguments after the program"
sys.exit(1)
even easier
if len(sys.argv) >= 0 | len(sys.argv) < 3 :
fieldindex = int(sys.argv[1])-1
else:
sys.exit("enter up to 2 args")