0

The goal of the code is that whenever user gives a line of string as input, the program should print all paths to every file, directory, sub directory that are under consideration (which means the input string is included in the file or directory name or included in the file-paths)

The problem of this code is that whenever I run the whole program, the "alist.extend(check_name(name,str(f)))" in the check_name function will run too many times that exceeds the default recursive limit and change the default number is not gonna help.

I tried to increase the default recursive limit to 2000, 3000, 4000, but it still didn't work. When i increased to 100000, it crashed.

def aopen(a:str,b:str) -> list:
    path = Path(b)
    alist =[]
    for p in path.iterdir():
        if p.is_file():
            alist.append(p)
        elif p.is_dir():
            alist.append(p)
            alist.extend(aopen(a,p))
    return alist

def check(name:str,path:str) -> list:
    alist=[]
    path0 = Path(path).cwd()
    for f in path0.iterdir():
        if name in str(f):
            if f.is_file():
                alist.append(f)
            elif f.is_dir():
                alist.extend(aopen(name,str(f)))
        elif name not in str(f) and f.is_dir():
            alist.extend(check(name,str(f)))
    return alist

def p():
    alist = []
    while True:
        link = input()
        if link == 'done-for-now':
            break
        alist = check(link,'..')
        if alist == []:
            print("NOT FOUND")
            continue
        else:
            for i in alist:
                print(i)

p()

The expected output should be a list of paths printed in the shell.

Calder White
  • 1,287
  • 9
  • 21
  • So, to get this right: You are checking if a string is in the names to all the paths to all the files in a certain directory, yes? – Calder White Jul 11 '19 at 02:03

1 Answers1

0

This can be accomplished with the os module in python. Specifically, os.walk. It returns all the paths to files within a given directory. The checking function would look like this.

import os

def check_dir(name, d):
    # get all the file paths
    paths = [[x[0] + z for z in x[2]] for x in os.walk(d)]

    # check if name is in the path
    good_paths = []
    for i in paths:
        if name in i:
            good_paths.append(i)

    return good_paths

Here's a good answer on os.walk: answer

Calder White
  • 1,287
  • 9
  • 21