I meet some problems here. I can search all the directory and print it out, but I just cannot find the longest path. Here is my terminal stats.
14102 1 1 /home/bob/Desktop
import os
def DFS(path, dirCallback = None, fileCallback = None):
stack = []
ret = []
stack.append(path);
while len(stack) > 0:
tmp = stack.pop(len(stack) - 1)
if(os.path.isdir(tmp)):
ret.append(tmp)
for item in os.listdir(tmp):
stack.append(os.path.join(tmp, item))
if dirCallback:
dirCallback(tmp)
elif(os.path.isfile(tmp)):
ret.append(tmp)
if fileCallback:
fileCallback(tmp)
return ret
def print_directory(path):
print "dir: " + path
def print_file(path):
print "file: " + path
d = DFS('~/', print_directory, print_file)
print max([x for x in d ],key=lambda x: x.count("/"))
I just have not idea why it cannot print out the longest path here. I am just the new to python and also I am a English learner, if I make any misunderstand, please let me know.
UPDATE: I can find the longest path right now, but I cannot use '~/' to find the directory.
Traceback (most recent call last):
File "Search.py", line 29, in <module>
print max([x for x in d ],key=lambda x: x.count("/"))
ValueError: max() arg is an empty sequence
this is Error message.