Python Forum

Full Version: add search bar = search for input in all computer directory
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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.
(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]):"
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
(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
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 :-)
Thanks Wuf
Pages: 1 2