Find a file with a word in its title or of a type

This code will make you find all the files that has in their title a word (or more) or that have an extension like .py, .txt etc. It will produce an html file that will open to show the list of files.

import os

def searchfiles(wit, folder):
    "Create a txt file with all the file of a type"
    outputfile = wit + "_found.html"
    with open(outputfile, "w", encoding="utf-8") as txt_file_with_list:
        fn_list = ""
        for r, d, f in os.walk(folder):
            fn_list += f"<h3>{r}</h3>"
            for file in f:
                if wit in file:
                    site = f"{r}\\{file}"
                    fn_list += f"<br><a href={site}>{site}</a>"
    
        txt_file_with_list.write(fn_list)
    os.startfile(outputfile)
    return fn_list

# you can put the extension or any word in title
searchfiles('.py', 'H:\\pylearning\\')

In this version you have only the list of files in a txt file.

import os

def searchfiles(wit, folder):
    "Create a txt file with all the file of a type"
    outputfile = wit + "_found.txt"
    with open(outputfile, "w", encoding="utf-8") as txt_file_with_list:
        fn_list = ""
        for r, d, f in os.walk(folder):
            fn_list += f"\n{'-'*30}\n{r}\n{' '*30}\n"
            for file in f:
                if wit in file:
                    fn_list += f"\t{r}\\{file}\n"
    
        txt_file_with_list.write(fn_list)
    os.startfile(outputfile)
    return fn_list

# you can put the extension or any word in title
searchfiles('.py', 'H:\\pylearning\\')

In this version we made a little GUI with tkinter

import os
from operator import itemgetter
import tkinter as tk

def listOfFiles(wit, folder):
    """args: 1) word to search
    2) folder to start search"""
    outputfile = wit + "_found.txt"
    with open(outputfile, "w", encoding="utf-8") as txt_file_with_list:
        fn_list = ""
        rfn_list = []
        for r, d, f in os.walk(folder):
            fn_list += f"\n---------------------------------\n{r}\n\n"
            for file in f:
                if wit in file:
                    fn_list += f"\t{r}\\{file}\n"
                    rfn_list.append([r,file])
    
        txt_file_with_list.write(fn_list)
    os.startfile(outputfile)
    return rfn_list

def curselect(evt):
    value=str((lb.get(lb.curselection())))
    os.startfile(value)

# ---------------------- G.U.I. --------------------------
root = tk.Tk()
root.title("SearchFile")
root.geometry("400x400+400+400")
scrollbar = tk.Scrollbar(root, orient=tk.VERTICAL)
lb = tk.Listbox(root, yscrollcommand=scrollbar.set)
scrollbar.config(command=lb.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
lb.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
lb.bind('<<ListboxSelect>>', curselect)
fn_list = sorted(listOfFiles(".py","H:\\pylearning\\"), key=itemgetter(1), reverse=True)



for f in fn_list:
    lb.insert(0,f[0]+"\\"+f[1])

root.mainloop()
# you can put the extension or any word in title
#searchfiles('.py', 'H:\\pylearning\\')

 

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.