我正在python()中创建一个文件/文件夹搜索器,就像窗口浏览器搜索栏一样),但我意识到“真正的路径”#39;只查找一个具有该名称的文件夹,如何搜索具有给定名称的所有文件夹,而不是搜索其中的内容,但我可以在哪里找到这些文件夹和文件?
import webbrowser
from tkinter import *
import os
tk = Tk()
打开一个tk
web1 = 'www.'
web2 = '.com'
en = Entry(tk)
en.grid()
'''types = input('What platform do you want to search? ')'''
旧输入法
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()
答案 0 :(得分:0)
感谢您将问题编辑成一个好问题。有一个明确的问题和明确的问题。 1
我不完全确定您要对搜索结果做什么。在下面的代码中,我只是打印出文件。
代码没有任何问题。所需要的只是一点点清理和删除。
我对代码所做的更改:
删除了重复的函数fi1
,finding1
和注释的代码行。还将Button的一项功能从fi1
更改为fi
。
删除了行if os.path.exists(term) == True:
,因为它阻止了搜索执行,所以我删除了它。检查这一点也没有意义,因为term
是用户输入的内容,而且这不是完整路径。
我没有在函数内部打印result
,而是将其返回并稍后打印出它的输出。
删除了行term = term
,因为不需要。
我通过删除字符find_all(term, r'C:\Users\MUM')
更改了行r
,并添加到我自己的目录中进行搜索。
我建议不要在find_all
函数中放入太大的目录。如果目录中包含太多文件夹,文件等,则需要很长时间。此外,代码中的路径不是真正的路径,请将其更改为适用于您的计算机的路径。
注意:我还没有测试过在Google和互联网上搜索的代码,因此我不确定这是否也有效。我测试的只是与搜索文件夹中的文件这个问题相关的代码。
这是固定代码:
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()
我希望这个答案对您有所帮助,如果您有任何其他问题,请随时在下面发表答案!