当字符串与索引匹配时 if 语句代码中没有 else 块按预期工作。但是当添加其他部分时,它总是显示“无字符串”
import sys
def string_search_index():
'''
This function search a string in a file through index and gives the result.
'''
if len(sys.argv) != 3:
print "Enter Two Arguments Only"
sys.exit(1)
stringsrch = sys.argv[2]
#size = len(sys.argv)
file_name = open("passwd", "r")
#print "No of Argu ", size
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
for store_file in file_name:
temp = store_file.split(":")
search = temp[fieldindex]
#print search
if stringsrch in search:
print store_file
else:
print "No String"
sys.exit(1)
string_search_index()