1

I'm working on a project that's just a super basic command line file browser. The problem is that I'm getting Max recursion depth exceeded errors. I'd like to convert this to at least a somewhat tail recursive thing. Here's the code:

import os, subprocess, itertools

def open_file(file, cwd):
    ''.join(file)
    if os.path.isdir(cwd + '/' + file) and '.app' not in file:
        os.chdir(cwd + '/' + file)
        print os.getcwd()
        filter_dir()
    elif '.app' in file:
        subprocess.call(['open', cwd + '/' + file])
    else:
        subprocess.call(['open', cwd + '/' + file])
        return

def input_checks(input, cwd):
    if input != '' and input != '../' and input[0] != '!' and input[0] != '.' and '<' not in input:
       out = itertools.ifilter(lambda x: input in x and '.' not in x, os.listdir(cwd))
    elif input == '../':
        print '../'
        cwd = os.chdir('../')
        filter_dir()
    elif input and input[0] == '!':
       out = itertools.ifilter(lambda x: input[1:] not in x, os.listdir(cwd))
    elif input and input[0] == '.':
       out = itertools.ifilter(lambda x: input[1:] in x, os.listdir(cwd))
    else:
       out = itertools.ifilter(lambda x: '.' not in x, os.listdir(cwd))
    return out

def filter_dir(input=''):
    cwd = os.getcwd()
    files = []
    out = input_checks(input, cwd)
    files = [i for i in out]
    for i in files:
        print i + '\t',
    for i in files:
        if i.lower() == input.lower():
            print i
            open_file(input, cwd)
            return
    if len(files) < 2:
        open_file(''.join(files), cwd)
        return
    print
    if '!' in input or '.' in input or '<' in input :
        input = ''
    print input,
    input = input + raw_input()
    print
    filter_dir(input)

I've tried two different solutions[1][2]. With both of them, the function ran once and then stopped. Can this be made to be tail recursive in Python?

[1] http://paulbutler.org/archives/tail-recursion-in-python/

[2] http://kylem.net/programming/tailcall.html

Jonathan
  • 539
  • 4
  • 15
  • Consider the following previous StackOverflow question which directly addresses the task that you appear to be trying to achieve ... namely: "walking a directory-tree in Python.": http://stackoverflow.com/questions/6639394/what-is-the-python-way-to-walk-a-directory-tree – Mike Robinson May 08 '15 at 01:24
  • 2
    While you might be able to make it tail-recursive - I haven't read your code, so I don't know - making it tail-recursive won't solve your problem. Python doesn't do tail call elimination. – user2357112 May 08 '15 at 01:27
  • Not exactly. I'm aware of os.walk and chose not to use it in this case. I want this to be really fast, so I'm only listing the contents of the current directory, and then using os.chwd to move around. – Jonathan May 08 '15 at 01:28
  • You could try [`scandir()`](https://www.python.org/dev/peps/pep-0471/), the eventual (Python 3.5) replacement for `walk()` (it's several times faster). – TigerhawkT3 May 08 '15 at 01:46

0 Answers0