简体   繁体   English

递归目录树python

[英]recursive directory tree python

def mapping(list_of_files , list_of_folders, max1):
    print max1
    max1 += 1

    if len(list_of_folders) == 0:
        folder = os.walk(os.getcwd()).next()[1]
        files = os.listdir(os.getcwd())
    elif len(list_of_files) != 0:
        print list_of_folders[0]
        ## for some reason this of line of code stops working without error on 20150 iteration 
        folder = next(os.walk(os.getcwd() +'/' + list_of_folders[0]))[1]
        #folder = os.walk(os.getcwd() +'/' + list_of_folders[0] ).next()[1]
        #print os.listdir(os.getcwd() + '/' +list_of_folders[0])
        files = os.listdir(os.getcwd() + '/' +list_of_folders[0])


    length = len(folder)
    length1 = len (files)

    y = 0
    if folder == files:
        del files[:] 
    else :
        for x in range(length):
            while y != length1:
                if files[y] == folder[x]:
                    files.pop(y)
                    length1 = length1 - 1
                    y = 0
                y += 1
            y = 0

    #print folder
    #print files

    if len(list_of_folders) == 0:
        list_of_files = add_to_main_lists(list_of_files,'', files)
        #print list_of_files
        list_of_folders = add_to_main_lists(list_of_folders, '', folder)
        #print list_of_folders
    else:
        list_of_files = add_to_main_lists(list_of_files,list_of_folders[0], files)
        #print list_of_files
        list_of_folders = add_to_main_lists(list_of_folders, list_of_folders[0], folder)
        #print list_of_folders
        list_of_folders.pop(0)


    if len(list_of_folders) == 0:
        print "got to here"
        return  list_of_files 
    else:
        print "file length: " + str(len(list_of_files))
        print "folder length: " + str(len(list_of_folders))
        mapping(list_of_files , list_of_folders, max1)

    return list_of_files

list_of_files = []
list_of_folders = []
list_of_files = mapping(list_of_files, list_of_folders , max1)
print (list_of_files)

The file is supposed to map out the folder the file is in and all of its sub directory. 该文件应该映射出文件所在的文件夹及其所有子目录。 For some reason when the code runs its 20150 iteration, the line folder = next(os.walk(os.getcwd() +'/' + list_of_folders[0]))[1] terminates the code and doesn't throw any error. 由于某些原因,当代码运行其20150迭代时,行文件夹= next(os.walk(os.getcwd()+'/'+ list_of_folders [0]))[1]会终止代码,并且不会引发任何错误。 I'm at a loss. 我很茫然。 I run the code using sudo. 我使用sudo运行代码。

I can see two things in the code: 我可以在代码中看到两件事:

  • You're calling .next() twice on the generator in the same iteration. 您在同一迭代中两次在生成器上调用.next() I don't think this is what you want to do, your iteration will be parsing two different os.walk outputs. 我不认为这是您想要做的,您的迭代将解析两个不同的os.walk输出。 Save the output of os.walk(os.getcwd()).next() in a var and use that. os.walk(os.getcwd()).next()的输出保存在var中并使用它。

  • You're processing the list of files only if there's no folders in the current levels, because you're using elif to do list_of_files . 您仅在当前级别中没有文件夹的情况下才处理文件列表,因为您正在使用elif来执行list_of_files You should use if since you want to process it anyway. 您应该使用if因为您仍然要处理它。

Additionally, keep in mind that the end of the iterator throws a StopIteration exception, so this is likely what's happening. 此外,请记住,迭代器的末尾会引发StopIteration异常,因此这很可能正在发生。 You need to handle that exception. 您需要处理该异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM