1

The aim is to have the user input a directory and a word in a tkinter GUI. The program searches for this word in the directory/subdirectories. If the word is found the path of the file should be printed. The number of directories and files searched should also be printed + the occurences of the word. The code below might have several issues, but gives me UnboundLocalError: local variable 'folders' referenced before assignment - any ideas?

import os
import tkinter as tk

results = []
folders = 0  
files = 0
words = 0

def recSearch(path,word):
    foldername = os.listdir(path)  
    for i in foldername:
        if os.path.isdir(f"{path}\\{i}"): 
            results.append(f"{path}\\{i}") 
            folders += 1 
            recSearch((f"{path}\\{i}"),word) 
        else: 
            with open(os.path.join(path, i), "r") as fileObject:
                for j in fileObject: 
                    if word in j:
                        files += 1
                        words += j.count(word) 
def click():
    recSearch(path.get(), word.get()) 
    text.insert('end', "Search start.\n ------------------\n")   
    for i in results:
        text.insert('end', i + "\n")
    text.insert('end', '------------------\n')
    text.insert('end', f"Searched {folders} directories and found {files} files with {words} occurences of {word.get()}")

window = tk.Tk()
window.title("Find word")
path = tk.StringVar()
entry = tk.Entry(window, textvariable=path, width=50)
entry.insert(-1,"Directory or filename")
word = tk.StringVar()
word_entry = tk.Entry(window, textvariable=word, width=20)
word_entry.insert(-1, "Word")
search_button = tk.Button(window, text="Search", width=15, command=click)      
text = tk.Text(window, height=50, width=180)
entry.grid(row=0, column=0)
word_entry.grid(row=0, column=1)
search_button.grid(row=0, column=2)
text.grid(row=1, columnspan=3)
window.mainloop()
Amarillo
  • 59
  • 6
  • 1
    `folders` inside `def recSearch(path,word):` is a _local_ variable that is named the same as your global one. See dupe about scoping. You either need to initialize it before incrementing if you want the local variabel or use the global keyword to make python know that you want to modify the outer scope global variable. Probably better to refacture your code to not depend on globals (would be my choice - globals suck) – Patrick Artner Mar 06 '22 at 12:06
  • 1
    Try making `folders` global in the `recSearch()` function – Sister Coder Mar 06 '22 at 12:08
  • 2
    Work on your [mre] btw. you could have deleted ALL the TK related stuff and just left the one function and a demo-call with data to `recSearch(.)`. – Patrick Artner Mar 06 '22 at 12:10
  • Thanks! Works fine after I made tha variables global. @PatrickArtner, not sure if I understood what you meant regarding the TK issues? – Amarillo Mar 06 '22 at 14:10
  • 1
    You have no issues that need TK to be inside this question - so all the TK related stuff is FLUFF and should not be part of a [mre] that describes your problem => _minimal_ – Patrick Artner Mar 06 '22 at 14:16
  • 1
    `k = 42; def func(): k += 1; func()` is about as minimal as you can do it to get the same error. – Patrick Artner Mar 06 '22 at 14:18

0 Answers0