Python Forum
[Tkinter] add search bar = search for input in all computer directory
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] add search bar = search for input in all computer directory
#11
fw = FileWalker('C:/') the root node.
Therein lies the problem, Microsoft has numerous directories under C: which the user does not have access to.
The code can be changed to skip these directories (maybe), but using glob, maybe not. Have to think about that.
On the other hand, you should be able to use it on any trees that you created as you will have access.
Reply
#12
(Apr-13-2019, 10:31 PM)Larz60+ Wrote: fw = FileWalker('C:/') the root node.
Therein lies the problem, Microsoft has numerous directories under C: which the user does not have access to.
The code can be changed to skip these directories (maybe), but using glob, maybe not. Have to think about that.
On the other hand, you should be able to use it on any trees that you created as you will have access.
import os
import stat
import glob
from pathlib import Path
 
 
class FileWalker:
    '''
    startdir = path relative to current path, example '../data' or
               use absolute path
    '''
    def __init__(self, startdir):
        # Anchor starting directory
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
        fileinfo = self.walk_node(startdir)
 
        # print first 10 entries.
        for n, entry in enumerate(fileinfo):
            if n > 10:
                break
            if stat.S_ISDIR(entry[1][0]):
            elif stat.S_ISREG(entry[1][0]):
            # You can check for all possibilities.
 
    def find_file(self, filename):
        for entry in self.fileinfo:
            if filename == entry[0].name:
 
    def walk_node(self, node, display=False):
        p = Path(node)
        fileinfo = []
        for file in p.glob('**/*'):
            fileinfo.append([file.resolve(), file.stat()])
        return fileinfo
 
if __name__ == '__main__':
    fw = FileWalker('C:/Users/Nelson')
    fw.find_file('Delaware.html')
    fileinfo = self.walk_node(startdir)
Based on your code, this is the changes i need to make? It gives my error on "elif stat.S_ISREG(entry[1][0]):"
Reply
#13
if stat.S_ISDIR(entry[1][0]):
            elif stat.S_ISREG(entry[1][0]):
This illegal, you need to do something between the if and elif
as a temporary measure, you could use pass than add code later when you know what you want to do
you can query stat.S_ISREG directly, don't need to check for ISDIR unless you need to know:
if stat.stat.S_ISREG(entry[1][0]):
    do something
Reply
#14
(Apr-13-2019, 10:58 PM)Larz60+ Wrote:
if stat.S_ISDIR(entry[1][0]):
            elif stat.S_ISREG(entry[1][0]):
This illegal, you need to do something between the if and elif
as a temporary measure, you could use pass than add code later when you know what you want to do
you can query stat.S_ISREG directly, don't need to check for ISDIR unless you need to know:
if stat.stat.S_ISREG(entry[1][0]):
    do something
thanks man, but that stuff is another level for me. i have no ideia what to put and where to put it. The other one, i did, it takes a bit more work indicating every path, but the job will be done. For me to understand this code i will need you to display a full code and where to change for my computer paths for me to get it, since you have more to do, appreciated anyways, but you can close this post. Thanks
Reply
#15
Hi Francisco

Play around with the following script:
import os, sys
from stat import *

def walktree(top, callback):
    '''recursively descend the directory tree rooted at top,
       calling the callback function for each regular file'''

    for f in os.listdir(top):
        pathname = os.path.join(top, f)
        mode = os.stat(pathname).st_mode
        
        if S_ISDIR(mode):
            # It's a directory, recurse into it
            callback(os.path.basename(pathname), "Directory")
            walktree(pathname, callback)
            
        elif S_ISREG(mode):
            # It's a file, call the callback function
            callback(os.path.basename(pathname), "File")
            
        else:
            # Unknown file type, print a message
            print('Skipping %s' % pathname)

def walktree_callback(item_name, item_type):
    print("{}: {}".format(item_type, item_name, ))

if __name__ == '__main__':
    startdir = os.getcwd() # Here place your own startdir path!
    walktree(startdir, walktree_callback)
wuf :-)
Reply
#16
Thanks Wuf
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Text.search() regexp not working rfresh737 11 1,068 Apr-16-2021, 06:56 PM
Last Post: rfresh737
  Listbox search code partially works chesschaser 9 1,705 May-05-2020, 01:08 PM
Last Post: chesschaser
  tkinter search box DT2000 3 4,681 Apr-08-2020, 05:59 AM
Last Post: DT2000
  [Tkinter] GUI help for search engine carzymind 0 1,268 Sep-27-2019, 10:49 AM
Last Post: carzymind
  [Tkinter] Problem with tkinter search widget poopcupine 1 1,616 Mar-25-2019, 08:24 AM
Last Post: Larz60+
  Python, Tkinter. How to search a specific file in a folder Roo 3 1,719 Feb-13-2019, 10:41 AM
Last Post: Larz60+
  [WxPython] Search window position in Trelby blackclover 20 5,244 May-07-2018, 05:08 AM
Last Post: blackclover
  [Tkinter] THow to search a string in a file and display it in listbox by Tkinter mosavit 2 3,858 Jun-21-2017, 08:02 AM
Last Post: buran
  [Tkinter] Search engine MeeranRizvi 0 3,777 Jan-24-2017, 08:21 AM
Last Post: MeeranRizvi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020