python搜索功能出错

时间:2014-06-02 08:24:13

标签: python

我正在尝试构建一个搜索功能,搜索文本文件并打印搜索字符串,如果在文本文件中找到该字符串。如果用户第二个字符串(quotesearch2)为空,则只搜索并打印第一个字符串(quotesearch1)。为什么我不断为第16行和第23行获取2个语法错误消息(代码检查器在第105行之后停止,因此在此之后可能会有更多)。请原因,我只是初学者,我不想要替代代码完成该功能,我只想解决错误。

错误是:

  

SyntaxError:语法无效

def search():
    import os.path
    print "you chose the search option"
    validchars = "-.() %s%s" %(string.ascii_letters , string.digits)
    thisDir = raw_input("What directory would you like to search ? Not: Any invalid characters will be stripped ") #ask user to choose a directory for multi OS compatibility 
    thisDir = ''.join(y for y in filesearch if y in validchars)
    filesearch = raw_input("What file would you like to search ? Note: Any invalid characters will be stripped: ") #ask user what file they would like t search from 
    filesearch = ''.join(x for x in filesearch if x in validchars)
    fullpath = os.path.join(thisDir, filesearch) #create full path for all operating system compatibility 
    while os.path.isfile(fullpath) == false: #check if the file doesnt exist, if it doesnt alert the user
        print "sorry the file you chose does not exist"  
        thisDir = pickAFolder
        filesearch = raw_input("What file would you like to search ? Note: Any invalid characters will be stripped: ")
        filesearch = ''.join(x for 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 the found variable is equal to false ask the user what they would like to search 
            quotesearch1 = raw_input("whats the first string you would you like to search ?: ")
            quotesearch2 = raw_input("whats the second string you want to search for ?: "
            for line in f: #for each line in the quote file if the quotesearch1 (usersfirst search) is in the line, print it along with a blank line
                if quotesearch2 != " " or "":
                    if quotesearch1 and quotesearch2 in line:
                        print line
                        print "\n"
                        linecount = line + 1  # variable to track the amount of lines printed
                elif quotesearch2 == " " or "":     
                    if quotesearch1 in line:
                        print line
                        print "\n"
                        linecount = line + 1    
            found = true
            if linecount == 0 and found == true : # if variable == 0 the quote search was not in the file and if the search was completed
                print "sorry the search could not be found" # print the quote search cold not be found
                Menu = true
                fclose(fullpath)
        elif found == true:
            print "search complete"

1 个答案:

答案 0 :(得分:1)

while os.path.isfile(fullpath) == false: 
 elif os.path.isfile(fullpath) == true:

您在Python中使用F资金False,在T使用资本True

您错过了")"

行的结束quotesearch2 = raw_input("whats the second string you want to search for ?: "
if quotesearch2 != " " or "":

应为if quotesearch2 != " " or quotesearch2 != "":

 elif quotesearch2 == " " or "": 

应为elif quotesearch2 == " " or quotesearch2 == "":

您没有检查quotesearch2是否不等于“”

In [7]: i=10


In [8]: if  i  == 9 or 4:
            print "weird"
   ...:     
       weird

In [9]: if  i  == 9 or i == 4:
        print "weird"
   ...:     

In [10]:

即使print "weird"不等于i,您也会看到10被执行,但是当我使用or i == 4:时它没有。{/ p >