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/