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
#8
(Apr-13-2019, 12:37 AM)Larz60+ Wrote: the following code will search from starting directory to last node.
you can find values for stat here: https://docs.python.org/3/library/stat.html

with this, you can test each entry for file type (directory, regular file, symbolic link, etc,)
and use what you need as they are all presented in the list returned from walk node:

You can insert what you need into your tkinter search algorithm
import os
import stat
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
            print(f'\nabspath: {entry[0]}')
            print(f'stats:   {entry[1]}')
            if stat.S_ISDIR(entry[1][0]):
                print(f'{entry[0].name} is a directory')
            elif stat.S_ISREG(entry[1][0]):
                print(f'{entry[0].name} is a regular file')
            # You can check for all possibilities.

    def find_file(self, filename):
        print(f'Searching for {filename}')
        for entry in self.fileinfo:
            if filename == entry[0].name:
                print(f'file found in {entry[0].resolve()}')

    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('/home/larz60/Documents/TestFileWalker/data')
    print('\n\n')
    fw.find_file('Delaware.html')
sample run from above (first 10 itens in array)
Output:
abspath: /home/larz60/Documents/TestFileWalker/data/control stats: os.stat_result(st_mode=16895, st_ino=71960773, st_dev=2049, st_nlink=2, st_uid=1000, st_gid=1000, st_size=4096, st_atime=1555110604, st_mtime=1550099077, st_ctime=1555110549) control is a directory abspath: /home/larz60/Documents/TestFileWalker/data/www2.census.gov.old stats: os.stat_result(st_mode=16895, st_ino=71960776, st_dev=2049, st_nlink=3, st_uid=1000, st_gid=1000, st_size=4096, st_atime=1555110604, st_mtime=1551005107, st_ctime=1555110556) www2.census.gov.old is a directory abspath: /home/larz60/Documents/TestFileWalker/data/html_old stats: os.stat_result(st_mode=16895, st_ino=71960656, st_dev=2049, st_nlink=2, st_uid=1000, st_gid=1000, st_size=4096, st_atime=1555110549, st_mtime=1550613985, st_ctime=1555110549) html_old is a directory abspath: /home/larz60/Documents/TestFileWalker/data/json stats: os.stat_result(st_mode=16895, st_ino=71960655, st_dev=2049, st_nlink=2, st_uid=1000, st_gid=1000, st_size=4096, st_atime=1555110604, st_mtime=1549856578, st_ctime=1555110548) json is a directory abspath: /home/larz60/Documents/TestFileWalker/data/tmp stats: os.stat_result(st_mode=16895, st_ino=69209097, st_dev=2049, st_nlink=3, st_uid=1000, st_gid=1000, st_size=4096, st_atime=1555110541, st_mtime=1551742394, st_ctime=1555110541) tmp is a directory abspath: /home/larz60/Documents/TestFileWalker/data/tmp_old stats: os.stat_result(st_mode=16895, st_ino=71960713, st_dev=2049, st_nlink=2, st_uid=1000, st_gid=1000, st_size=4096, st_atime=1555110604, st_mtime=1550613985, st_ctime=1555110549) tmp_old is a directory abspath: /home/larz60/Documents/TestFileWalker/data/docs stats: os.stat_result(st_mode=16895, st_ino=71959774, st_dev=2049, st_nlink=6, st_uid=1000, st_gid=1000, st_size=4096, st_atime=1555110604, st_mtime=1551614378, st_ctime=1555110548) docs is a directory abspath: /home/larz60/Documents/TestFileWalker/data/0README_PL.doc stats: os.stat_result(st_mode=33279, st_ino=69209092, st_dev=2049, st_nlink=1, st_uid=1000, st_gid=1000, st_size=98816, st_atime=1555110540, st_mtime=1551005110, st_ctime=1555110540) 0README_PL.doc is a regular file abspath: /home/larz60/Documents/TestFileWalker/data/www2.census.gov stats: os.stat_result(st_mode=16895, st_ino=69209211, st_dev=2049, st_nlink=95, st_uid=1000, st_gid=1000, st_size=4096, st_atime=1555110542, st_mtime=1551563760, st_ctime=1555110542) www2.census.gov is a directory abspath: /home/larz60/Documents/TestFileWalker/data/html stats: os.stat_result(st_mode=16895, st_ino=69209093, st_dev=2049, st_nlink=2, st_uid=1000, st_gid=1000, st_size=4096, st_atime=1555110604, st_mtime=1551742394, st_ctime=1555110540) html is a directory abspath: /home/larz60/Documents/TestFileWalker/data/ftp stats: os.stat_result(st_mode=16895, st_ino=71959772, st_dev=2049, st_nlink=2, st_uid=1000, st_gid=1000, st_size=4096, st_atime=1555110604, st_mtime=1551573910, st_ctime=1555110542) ftp is a directory Searching for Delaware.html file found in /home/larz60/Documents/TestFileWalker/data/html_old/Delaware.html file found in /home/larz60/Documents/TestFileWalker/data/tmp_old/Delaware.html
when done playing with this code, remove all print statements, and the call to run (in __init__)
fileinfo = self.walk_node(startdir)
and then import the class into your program and use what you want.

** Edited 8:33 PM EDT **
added find_file routine and test for same
Thanks for your tip, since your code gives me error. The person with lots of files, should put them in folders, that's why i created different code to do that. Then it's more easy for her to search for the any intended file
Reply


Messages In This Thread
RE: add search bar = search for input in all computer directory - by francisco_neves2020 - Apr-13-2019, 03:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Search data in treeview without search button TomasSanchexx 3 1,678 Aug-12-2023, 03:17 AM
Last Post: deanhystad
  search is not finding documentation i want Skaperen 1 922 Aug-26-2022, 08:17 AM
Last Post: Gribouillis
  [PyQt] [Solved]Display Search Results in QTable Extra 5 2,515 Jun-29-2022, 10:20 PM
Last Post: Extra
  [Tkinter] Text.search() regexp not working rfresh737 11 5,095 Apr-16-2021, 06:56 PM
Last Post: rfresh737
  Listbox search code partially works chesschaser 9 3,979 May-05-2020, 01:08 PM
Last Post: chesschaser
  tkinter search box DT2000 3 7,755 Apr-08-2020, 05:59 AM
Last Post: DT2000
  [Tkinter] GUI help for search engine carzymind 0 2,686 Sep-27-2019, 10:49 AM
Last Post: carzymind
  [Tkinter] Problem with tkinter search widget poopcupine 1 2,716 Mar-25-2019, 08:24 AM
Last Post: Larz60+
  Python, Tkinter. How to search a specific file in a folder Roo 3 3,461 Feb-13-2019, 10:41 AM
Last Post: Larz60+
  [WxPython] Search window position in Trelby blackclover 20 9,105 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