开发者

Search is printing results and "string not found" condition

Can someone explain to me why my conditional search statement is returning both results (it finds the string and prints the results to the screen and it prints "String Not Found"). I've made changes, but I must be overlooking something.

Code:

 if choice == '1':
         regex2  = re.compile(r'\s+')
         for root,dirnam开发者_Go百科e, files in os.walk(directory):
             for file2 in files:
                 if file2.endswith(".log") or file2.endswith(".txt"):
                    f=open(os.path.join(root, file2))
                    for i,line in enumerate(f.readlines()):
                        result2 = regex.search(re.sub(regex2, '',line))
                        if result2:
                           ln = str(i)
                           print "\nLine: " + ln
                           print "File: " + os.path.join(root,file2)
                           print "String Type: " + result2.group() + '\n'
                           temp.write('\nLine:' + ln + '\nString:' + result2.group() + '\nFile: ' + os.path.join(root,file2) + '\n')

                    else:
                       print "String not found!!!"
                    break  
                    f.close()    
         re.purge()


You have an indentation problem i think you want:

...
for i,line in enumerate(f.readlines()):
    result2 = regex.search(re.sub(regex2, '',line))
    if result2:
        ln = str(i)
        print "\nLine: " + ln
        print "File: " + os.path.join(root,file2)
        print "String Type: " + result2.group() + '\n'
        temp.write('\nLine:' + ln + '\nString:' + result2.group() + '\nFile: ' + os.path.join(root,file2) + '\n')

    else:  # <<<<<<<<<<<<<<<<<<<< HERE !!!!
        print "String not found!!!"
break
...


The else clause to a for loop is execute if the loop exits because the iterable it iterates over is exhausted, rather than due to a break statement. Since your loop doesn't include any break, the else clause will always be executed.

Here is an attempt at refactoring your code. It uses a generator function to generate the list of filenames and the fileinput module to take care of opening and closing the files. Your cod never explicitly closes any file due to the break immediatley before f.close().

def walk_dir(directory, extensions=""):
    for path, dirs, files in os.walk(directory):
        for name in files:
            if name.endswith(extensions):
                yield os.path.join(path, name)

whitespace  = re.compile(r'\s+')
for line in fileinput.input(walk_dir(directory, (".log", ".txt"))):
    result = regex.search(whitespace.sub('', line))
    if result:
        template = "\nLine: {0}\nFile: {1}\nString Type: {2}\n\n"
        output = template.format(fileinput.filelineno(),
                                 fileinput.filename(),
                                 result.group())
        print output
        temp.write(output)


Please refactor your code a bit - there's way too much indentation and mixing control structures. It will be easier to both spot and correct the problem this way.

For example - split the loop into traversing and checking:

def look_through(directory):
    found = 0
    for root, dirname, files in os.walk(directory):
        for filename in files:
            result = process_file(root, filename)
            if result is not None:
                found += 1
                yield result
    if found == 0:
        print 'blah, not found'

def process_file(...

Do you see the problem with previous code now? Any condition was only checked per-file and then again - per-directory. There was no global counter of results, or search state recorded.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜