Python Forum
[Tkinter] add search bar = search for input in all computer directory - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] add search bar = search for input in all computer directory (/thread-17441.html)

Pages: 1 2


RE: add search bar = search for input in all computer directory - Larz60+ - Apr-13-2019

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.


RE: add search bar = search for input in all computer directory - francisco_neves2020 - Apr-13-2019

(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]):"


RE: add search bar = search for input in all computer directory - Larz60+ - Apr-13-2019

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



RE: add search bar = search for input in all computer directory - francisco_neves2020 - Apr-14-2019

(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


RE: add search bar = search for input in all computer directory - wuf - Apr-14-2019

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 :-)


RE: add search bar = search for input in all computer directory - francisco_neves2020 - Apr-14-2019

Thanks Wuf