-1

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.

Dominique Fortin
  • 2,212
  • 15
  • 20
  • what os are you using? If using mac of linux try `ls = temp.split('/')`. To find the longest you can also use `print max([x for x in d ],key=lambda x: x.count("/"))` – Padraic Cunningham Jun 30 '14 at 21:13
  • OMG, the last one is so useful, thank you for the help. Yeah, I am using the linux, which is centOS. – user3791763 Jun 30 '14 at 21:22
  • so it worked ok? Paths in linux use `/` so `\\\` was splitting nothing – Padraic Cunningham Jun 30 '14 at 21:23
  • Yeah I have change my code a lot. But looks like I cannot use ~/ to find my directory. Any solutions? d = DFS('~/', print_directory, print_file) It just show the Traceback (most recent call last): File "Search.py", line 29, in print max([x for x in d ],key=lambda x: x.count("/")) ValueError: max() arg is an empty sequence – user3791763 Jul 01 '14 at 05:40

1 Answers1

1

This code should be fully working:

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

home = os.path.expanduser('~')
d = DFS(home, print_directory, print_file)


print max([x for x in d ],key=lambda x: x.count("/"))
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321