0

I am making a file/ folder searcher in python() like the window explorer search bar), but i realised that 'os real path ' only finds one folder with that name, how do i search for all folders with a given name, and not what is in them, but where i can find these folders and files?

import webbrowser
from tkinter import *
import os
tk = Tk()

opens a tk

web1 = 'www.'
web2 = '.com'
en = Entry(tk)
en.grid()

'''types = input('What platform do you want to search? ')'''

old input method

bind = 0

def work():
    global tk1, term, en1, tabUrl, end
    tk1 = Tk()
    en1 = Entry(tk1)
    en1.grid(row=1)
    b = Button(tk1, text='Go', command=fi1)
    b.grid(row=2)
    if types.upper() == 'GOOGLE':
        tabUrl = "https://google.com/search?q=";
        end = ("&cad=h")
        l = Label(tk1, text='What would you like to search?')
        l.grid(row=0)
        '''term = input("What would you like to search? ")'''

    if types.upper() == "YOUTUBE":
        tabUrl = "https://www.youtube.com/results?search_query=";
        '''term = input("What would you like to search? ")'''
        l = Label(tk1, text='What would you like to search?')
        l.grid(row=0)

    if types.upper() == 'INTERNET' or types.upper() == 'WEB' or types.upper() == 'THE WEB' or types.upper() == 'WEBSITE':
        l = Label(tk1, text='What website would you like to open?')
        l.grid(row=0)
    if types.upper() == 'COMPUTER':
        l = Label(tk1, text='What folder would you like to open?')
        l.grid(row=0)
    tk1.mainloop()

'''def find_all(name, path):
    for root, dirs, files in os.walk(path):
        for name in files:
            print(os.path.join(root, name))
        for name in dirs:
            print(os.path.join(root, name))'''
def find_all(name, path):
    result = []
    for root, dirs, files in os.walk(path):
        if name in files:
            result.append(os.path.join(root, name))
    print( result)

def fi(self):
    global term
    term = en1.get()
    if types.upper() == 'GOOGLE':
        term = term.replace(' ', '%20')
        webbrowser.open(tabUrl + term + end)
    if types.upper() == "YOUTUBE":
        term = term.replace(' ', '+')
        webbrowser.open(tabUrl + term)
    if types.upper() == 'INTERNET' or types.upper() == 'WEB' or types.upper() == 'THE WEB' or types.upper() == 'WEBSITE':
        if term.upper() == 'GOOGLE':
            webbrowser.open('www.google.com')

        else:
            webfind1 = term.find(web1)
            webfind2 = term.find(web2)
            if webfind1 >= 0 and webfind2 >= 0:
                webbrowser.open(term)
            else:
                tk1.destroy()
                tk2 = Tk()
                la = Label(tk2, text='Your website must have a ".com" and a "www."')
                la.grid()
                tk2.mainloop()
    if types.upper() == 'COMPUTER':
        term = term
        if os.path.exists(term) == True:
            find_all(term, r'C:\Users\MUM')
            #place = os.path.realpath(term)
            #print(os.path.realpath(term))
            #os.startfile(place)

def fi1():
    global term
    term = en1.get()
    if types.upper() == 'GOOGLE':
        term = term.replace(' ', '%20')
        webbrowser.open(tabUrl + term + end)
    if types.upper() == "YOUTUBE":
        term = term.replace(' ', '+')
        webbrowser.open(tabUrl + term)
    if types.upper() == 'INTERNET' or types.upper() == 'WEB' or types.upper() == 'THE WEB' or types.upper() == 'WEBSITE':
        if term.upper() == 'GOOGLE':
            webbrowser.open('www.google.com')

        else:
            webfind1 = term.find(web1)
            webfind2 = term.find(web2)
            if webfind1 >= 0 and webfind2 >= 0:
                webbrowser.open(term)
            else:
                tk1.destroy()
                tk2 = Tk()
                la = Label(tk2, text='Your website must have a ".com" and a "www."')
                la.grid()
                tk2.mainloop()
    if types.upper() == 'COMPUTER':
        term = term
        if os.path.exists(term) == True:
            find_all(term, r'C:\Users\MUM')
            #place = os.path.realpath(term)
            #print(os.path.realpath(term))
            #os.startfile(place)


def finding(self):
    global types, bind
    types = en.get()
    tk.destroy()
    work()
    tk1.bind('<Return>', fi)

def finding1():
    global types, bind
    types = en.get()
    tk.destroy()
    work()
    tk1.bind('<Return>', fi)
if bind == 0:
    tk.bind('<Return>', finding)
tk.mainloop()
  • 1
    Possible duplicate of [How to make python search the entire HDD](https://stackoverflow.com/questions/27629469/how-to-make-python-search-the-entire-hdd) – Micheal O'Dwyer Feb 11 '18 at 14:53
  • No I need it to show multiple results with the same name. I am find it but I need to know how to find other files with the same name – TheBajan_israelian Feb 11 '18 at 15:31
  • 1
    Unless I am reading the answer wrong, `os.walk` will go through all the files and paths in the directory that you give it. While it "walks" through the files, append the files that have the search name to a list. As in, don't stop execution of the for loop in that answer when you have found a match, keep executing and append the files with the same name to a list. – Micheal O'Dwyer Feb 11 '18 at 15:36
  • how would i do that – TheBajan_israelian Feb 11 '18 at 18:32
  • it shows me all files in that folder i need it to show me files that end in the given name, such as id it were python. c:/python, c:/python/python.txt, c:/docs/python.docs – TheBajan_israelian Feb 11 '18 at 18:47
  • After another quick search, I found the exact answer you are looking for. Here is the [link](https://stackoverflow.com/a/1724723/8390068). In future, make sure you research before you ask a question. More often than not, your question has an answer, especially if it is something simple or general like this. – Micheal O'Dwyer Feb 11 '18 at 19:13
  • yes that was helpful but how would i asign it to only find a certain name, in C:/, like os.filepath() – TheBajan_israelian Feb 11 '18 at 19:51
  • The answer only works for one name. It takes in the name, searches through all the files in the path, and appends the matches to a list. – Micheal O'Dwyer Feb 11 '18 at 20:07
  • Where would I put the name I am searching for – TheBajan_israelian Feb 11 '18 at 21:28
  • Look at the function `find_all(name, path)` in the answer that I linked to. Pass the name of the file first and then the path. E.g. `find_all("input.txt", "C:\"). – Micheal O'Dwyer Feb 11 '18 at 22:04
  • I added it to my code, but now it only searches when I type pyrhon, and it gives an endless list. It does not search for any other string – TheBajan_israelian Feb 12 '18 at 08:08
  • Use the `edit` link under your question to include your code and the input and output. I don't really understand what the problem is without seeing this information. – Micheal O'Dwyer Feb 12 '18 at 11:36
  • i have added my code – TheBajan_israelian Feb 12 '18 at 14:08

1 Answers1

0

Thank you for editing your question into a good one. There is an mcve and clear problem stated. +1

I am not entirely sure what you want to do with the results of the search. In the code below I just printed the files out.

There wasn't really anything wrong with the code. All that was needed was a little cleaning and deleting.

Changes I made to the code:

  • Removed the duplicate functions fi1, finding1 and the commented lines of code. Also changed one of the Button's functions from fi1 to fi.

  • Removed the line if os.path.exists(term) == True: because it was stopping the search from executing so I removed it. It also doesn't make sense to check this because term is what the user entered and this isn't a full path.

  • Instead of printing result inside the function, I returned it and printed the output of it's call later on.

  • Removed the line term = term, because it is not needed.

  • I changed the line find_all(term, r'C:\Users\MUM') by removing the character r and added in my own directory to search in.

I recommend not putting in a directory that is too large in the find_all function. It takes very long if the directory has too many folders, files, etc. Also, the path in the code is not a real path, change it to one for your computer.

Note: I haven't tested the code that searches on Google and the internet so I am not sure if that works too. All I tested was the code relating to this question about searching for files in a folder.

Here is the fixed code:

import webbrowser
from tkinter import *
import os
tk = Tk()

web1 = 'www.'
web2 = '.com'
en = Entry(tk)
en.grid()

bind = 0

def work():
    global tk1, term, en1, tabUrl, end

    tk1 = Tk()

    en1 = Entry(tk1)
    en1.grid(row=1)

    b = Button(tk1, text='Go', command=fi)
    b.grid(row=2)

    if types.upper() == 'GOOGLE':
        tabUrl = "https://google.com/search?q=";
        end = ("&cad=h")

        l = Label(tk1, text='What would you like to search?')
        l.grid(row=0)

    if types.upper() == "YOUTUBE":
        tabUrl = "https://www.youtube.com/results?search_query=";
        l = Label(tk1, text='What would you like to search?')
        l.grid(row=0)

    if types.upper() == 'INTERNET' or types.upper() == 'WEB' or types.upper() == 'THE WEB' or types.upper() == 'WEBSITE':
        l = Label(tk1, text='What website would you like to open?')
        l.grid(row=0)

    if types.upper() == 'COMPUTER':
        l = Label(tk1, text='What folder would you like to open?')
        l.grid(row=0)

    tk1.mainloop()

def find_all(name, path):
    result = []
    for root, dirs, files in os.walk(path):
        if name in files:
            result.append(os.path.join(root, name))

        # print(files)

    return result

def fi():
    global term
    term = en1.get()
    if types.upper() == 'GOOGLE':
        term = term.replace(' ', '%20')
        webbrowser.open(tabUrl + term + end)
    if types.upper() == "YOUTUBE":
        term = term.replace(' ', '+')
        webbrowser.open(tabUrl + term)
    if types.upper() == 'INTERNET' or types.upper() == 'WEB' or types.upper() == 'THE WEB' or types.upper() == 'WEBSITE':
        if term.upper() == 'GOOGLE':
            webbrowser.open('www.google.com')

        else:
            webfind1 = term.find(web1)
            webfind2 = term.find(web2)
            if webfind1 >= 0 and webfind2 >= 0:
                webbrowser.open(term)
            else:
                tk1.destroy()
                tk2 = Tk()
                la = Label(tk2, text='Your website must have a ".com" and a "www."')
                la.grid()
                tk2.mainloop()
    if types.upper() == 'COMPUTER': 
        print(find_all(term, "C:\\path\\to\\file"))


def finding(self):
    global types, bind
    types = en.get()
    tk.destroy()
    work()
    tk1.bind('<Return>', fi)


if bind == 0:
    tk.bind('<Return>', finding)

tk.mainloop()

I hope this answer helped you and if you have any further questions please feel free to post an answer below!

Micheal O'Dwyer
  • 1,237
  • 1
  • 16
  • 26
  • Thank you very much, I duplicated the functions for it to be able to be used with a button and enter – TheBajan_israelian Feb 12 '18 at 18:59
  • If this answered your question, please [accept it](https://stackoverflow.com/help/someone-answers) so others viewing this question know it has an answer and is resolved. Thanks! – Micheal O'Dwyer Feb 12 '18 at 19:57
  • how do i do that? – TheBajan_israelian Feb 12 '18 at 22:03
  • Click on the tick underneath the number of votes for this answer. If you click on the tick it shows that you accepted this answer. – Micheal O'Dwyer Feb 12 '18 at 22:04
  • See this [meta post](https://meta.stackexchange.com/questions/86978/how-do-i-accept-an-answer-on-stackoverflow) on how to accept an answer. – Micheal O'Dwyer Feb 12 '18 at 22:06
  • it doesnt print the results of the search – TheBajan_israelian Feb 14 '18 at 14:56
  • Make that you change the path from `C:\\path\\to\\file` to something that is a real path. If the path has too many files, the search takes very long and will not print it out until it is finished. I cannot do anything about that though. To see that it is searching uncomment the line `print(files)` in the code of my answer above. That will show you all the files it is searching through. – Micheal O'Dwyer Feb 14 '18 at 15:55