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] Search data in treeview without search button TomasSanchexx 3 1,523 Aug-12-2023, 03:17 AM
Last Post: deanhystad
  search is not finding documentation i want Skaperen 1 866 Aug-26-2022, 08:17 AM
Last Post: Gribouillis
  [PyQt] [Solved]Display Search Results in QTable Extra 5 2,386 Jun-29-2022, 10:20 PM
Last Post: Extra
  [Tkinter] Text.search() regexp not working rfresh737 11 4,910 Apr-16-2021, 06:56 PM
Last Post: rfresh737
  Listbox search code partially works chesschaser 9 3,759 May-05-2020, 01:08 PM
Last Post: chesschaser
  tkinter search box DT2000 3 7,610 Apr-08-2020, 05:59 AM
Last Post: DT2000
  [Tkinter] GUI help for search engine carzymind 0 2,628 Sep-27-2019, 10:49 AM
Last Post: carzymind
  [Tkinter] Problem with tkinter search widget poopcupine 1 2,641 Mar-25-2019, 08:24 AM
Last Post: Larz60+
  Python, Tkinter. How to search a specific file in a folder Roo 3 3,384 Feb-13-2019, 10:41 AM
Last Post: Larz60+
  [WxPython] Search window position in Trelby blackclover 20 8,889 May-07-2018, 05:08 AM
Last Post: blackclover

Forum Jump:

User Panel Messages

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