Learn practical skills, build real-world projects, and advance your career
import os
import time
def file_in_dir(file_name="mp4", drx="D://knowledge/"):
    if drx[-1] != "/":
        drx += "/"
    info, drxs, info_file = [], [], []
    try:
        files = os.listdir(drx)
    except:
        return [], 0
    
    for file in files:
        f_list = file.split(".")
        if len(f_list) > 1:
            info_file.append(file)
        else:
            drxs.append(file)
    
    for f in info_file:
        if f.endswith(file_name):
            info.append(f)
            
    f_count = len(info)
    
    for d in drxs:
        info_, f_count_ = file_in_dir(file_name, drx+d, depth+1)
        info += info_
        f_count = f_count+f_count_
    return info, f_count
%%time
file, n = file_in_dir("exe", "C://")
Wall time: 15.1 s
n
2165
import os
import time
import re
import datetime

def modifiedSearch(text="py", drx=".", location="", with_drx=True):
    """Search directories for files (Directories without Permission will be skipped) and returns the list of files
    
    Parameters:
        text (string): (default: "py") 
                Enter the text of file that you wand to search. It could be extension or
                any other string of the file. 
        drx (string):  (default: ".") 
                Enter the directory from where you want to start your search. Note: Starting from 
                root dirctory might be extensive.
        location (string): (optional) (default: "") 
                It asks for where to search for your text in filename. Provide "start"
                or "end" if you are sure that filename will start or end with the text respectively. Otherwise leave it.
        with_drx (bool): (optional) (default: True) 
                Appends directory of files before files in output list.
    """
    files = []
    if os.path.isdir(drx):
        try:
            in_dir = os.listdir(drx)
        except:
            in_dir = []
        if len(in_dir) == 0:
            return []
        for file in in_dir:
            join_drx = os.path.join(drx, file)
            if os.path.isdir(join_drx):
                files += modifiedSearch(text, join_drx, location, with_drx)
            else:
                searched_files = []
                if location == "end":
                    search = re.search(r"{}$".format(text), file)
                    if search is not None:
                        searched_files.append("   " + file)
                elif location == "start":
                    search = re.search(r"^{}".format(text), file)
                    if search is not None:
                        searched_files.append("   " + file)
                elif location == "":
                    search = re.search(r"{}".format(text), file)
                    if search is not None:
                        searched_files.append("   " + file)
                if with_drx == True:
                    if len(searched_files) != 0:
                        if searched_files[0] != ("drx: " + drx):
                            searched_files = ["drx: " + drx] + searched_files
                files += searched_files
        return files
help(modifiedSearch)
Help on function modifiedSearch in module __main__: modifiedSearch(text='py', drx='.', location='', with_drx=True) Search directories for files (Directories without Permission will be skipped) and returns the list of files Parameters: text (string): (default: "py") Enter the text of file that you wand to search. It could be extension or any other string of the file. drx (string): (default: ".") Enter the directory from where you want to start your search. Note: Starting from root dirctory might be extensive. location (string): (optional) (default: "") It asks for where to search for your text in filename. Provide "start" or "end" if you are sure that filename will start or end with the text respectively. Otherwise leave it. with_drx (bool): (optional) (default: True) Appends directory of files before files in output list.