1
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. I'm at a loss. I run the code using sudo.

6
  • 1
    How do you know this? Commented May 8, 2016 at 22:31
  • Ive used print function to narrow down to that line of code. Commented May 8, 2016 at 22:38
  • This is Python 2 or 3? What is this mapping function doing? What is max1? Commented May 8, 2016 at 22:43
  • python 2, max1 is used to keep track of the iterations. Mapping function is supposed to map out a folder and sub folder and store everything is a single list. An example is ['s3-bucket.py', '.c9/project.settings', '.c9/.nakignore', 'bucket/hello-world.html', 'bucket/Untitled', 'bucket/README.md', '.c9/metadata/tab19', '.c9/metadata/tab20', '.c9/metadata/tab0', '.c9/metadata/tab1', 'west/test/stats.py', '.c9/metadata/workspace/s3-bucket.py']. For some reason when the code goes through 20150 iteration the code terminates without an error. Commented May 8, 2016 at 22:47
  • What are you trying to do in the end ? Commented May 8, 2016 at 23:32

1 Answer 1

2

I can see two things in the code:

  • You're calling .next() twice on the generator in the same iteration. I don't think this is what you want to do, your iteration will be parsing two different os.walk outputs. Save the output of os.walk(os.getcwd()).next() in a var and use that.

  • 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. You should use if since you want to process it anyway.

Additionally, keep in mind that the end of the iterator throws a StopIteration exception, so this is likely what's happening. You need to handle that exception.

Sign up to request clarification or add additional context in comments.

2 Comments

How would I change the code to deal with all of these issues?
Im not receiving a StopIteration error the program just stops.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.