Error in python search function -
i getting trying build search function searches through text files , prints search string if string found in text file. if users second string (quotesearch2) empty first string searched , printed (quotesearch1). why keep getting 2 syntax error messages lines 16 , line 23 (the code checker stoped after line 105 there may more after point).excuse code, beginner, not want alternative code complete function, want errors resolved.
the error is:
syntaxerror: invalid syntax
def search(): import os.path print "you chose search option" validchars = "-.() %s%s" %(string.ascii_letters , string.digits) thisdir = raw_input("what directory search ? not: invalid characters stripped ") #ask user choose directory multi os compatibility thisdir = ''.join(y y in filesearch if y in validchars) filesearch = raw_input("what file search ? note: invalid characters stripped: ") #ask user file t search filesearch = ''.join(x x in filesearch if x in validchars) fullpath = os.path.join(thisdir, filesearch) #create full path operating system compatibility while os.path.isfile(fullpath) == false: #check if file doesnt exist, if doesnt alert user print "sorry file chose not exist" thisdir = pickafolder filesearch = raw_input("what file search ? note: invalid characters stripped: ") filesearch = ''.join(x x in filesearch if x in validchars) #strip invalid characters fullpath = os.path.join(thisdir, filesearch) elif os.path.isfile(fullpath) == true: f = open(fullpath, 'r') #open file found = false linecount = 0 while found == false: # while found variable equal false ask user search quotesearch1 = raw_input("whats first string search ?: ") quotesearch2 = raw_input("whats second string want search ?: " line in f: #for each line in quote file if quotesearch1 (usersfirst search) in line, print along blank line if quotesearch2 != " " or "": if quotesearch1 , quotesearch2 in line: print line print "\n" linecount = line + 1 # variable track amount of lines printed elif quotesearch2 == " " or "": if quotesearch1 in line: print line print "\n" linecount = line + 1 found = true if linecount == 0 , found == true : # if variable == 0 quote search not in file , if search completed print "sorry search not found" # print quote search cold not found menu = true fclose(fullpath) elif found == true: print "search complete"
while os.path.isfile(fullpath) == false: elif os.path.isfile(fullpath) == true:
you use capital f
false
in python , capital t
true
and missing closing ")"
on line quotesearch2 = raw_input("whats second string want search ?: "
if quotesearch2 != " " or "":
should if quotesearch2 != " " or quotesearch2 != "":
elif quotesearch2 == " " or "":
should elif quotesearch2 == " " or quotesearch2 == "":
you not checking if quotesearch2 not equal ""
in [7]: i=10 in [8]: if == 9 or 4: print "weird" ...: weird in [9]: if == 9 or == 4: print "weird" ...: in [10]:
you see print "weird"
gets executed though i
not equal 10
in second statement when use or == 4:
not.
Comments
Post a Comment