Python how to access subfolder of subfolders
Question: I am new to python and I have trouble accessing the math text-files within the
subfolders.

                    ![Hierarchy of the folder ](http://i.stack.imgur.com/oEhqn.png)

This is the code I have written so far:

    
    
    import os, sys
    for folder, sub_folders, files in os.walk(my_directory):
       for special_file in files:
          if special_file == 'math.txt'
             file_path = os.path.join(folder, special_file)
             with open(file_path, 'r+') as read_file
                counter += 1
                print('Reading math txt file' + str(counter))
    
                for line in read_file:
                   print(line)
    

I am not able to do print lines of all the `math.txt` files within all the
classes and all the schools and all the zones.

Before I had a version of the script that merged all the files, but some of
the log files were extremely large (combined > 16GB).


Answer: This seems to work for me. Only changes were those that @jdi, @MRAB and I were
indicating - missing colons and initializing the `counter` variable. Since
you're on Windows, you may want to make sure that you're properly specifying
your directory path.

    
    
    import os, sys
    
    # Specify directory
    # In your case, you may want something like the following
    my_directory = 'C:/Users/<user_name>/Documents/ZoneA'
    
    # Define the counter
    counter = 1
    
    # Start the loop
    for folder, sub_folders, files in os.walk(my_directory):
      for special_file in files:
        if special_file == 'math.txt':
          file_path = os.path.join(folder, special_file)
    
          # Open and read
          with open(file_path, 'r+') as read_file:
            print('Reading math txt file ' + str(counter))
    
            # Print the file
            for line in read_file:
               print(line)
    
            # Increment the counter
            counter += 1
    

