user3044431
user3044431

Reputation:

Trying to use glob to iterate through files in a folder in python

I keep just getting the data i want from the last file in the list. I appreciate the program working that much, but i would rather it worked through all of the files in the folder.

   import os
   import glob
   os.chdir('C:\Users\U2970\Documents\Arcgis\Text_files\Data_exports')
   for file in list(glob.glob('*.txt')):
       reader = open(file)

   from EXPORT_COLUMN_SET import *

   try:
        data = column_set(reader)
        data.start_data_printer()
   finally:
        reader.close()

Upvotes: 10

Views: 33950

Answers (2)

that other guy
that other guy

Reputation: 123470

The scope of the for loop defines what is done for each file. Everything after the for loop is only done once:

 import os                                                                   
 import glob                                                                 
 os.chdir('C:\Users\U2970\Documents\Arcgis\Text_files\Data_exports')         
 for file in list(glob.glob('*.txt')):                                       
     reader = open(file)               # <-- Things done for each file

 # Things done only once:                      
 from EXPORT_COLUMN_SET import *                                             

 try:                                                                        
      data = column_set(reader)                                              
      data.start_data_printer()                                              
 finally:                                                                    
      reader.close()   

You can indent the rest of the code to put it inside the for loop:

 import os                                                                   
 import glob                                                                 
 os.chdir('C:\Users\U2970\Documents\Arcgis\Text_files\Data_exports')       
 for file in list(glob.glob('*.txt')):                           
     reader = open(file)                  # ^                 
                                          # |                
     from EXPORT_COLUMN_SET import *      # |              
                                          # | Things now done for each file
     try:                                 # |                                 
          data = column_set(reader)       # |                              
          data.start_data_printer()       # |                              
     finally:                             # |                              
          reader.close()                  # v

Upvotes: 14

Karoly Horvath
Karoly Horvath

Reputation: 96266

After the loop reader will contain the last opened file, then you process that single file.

Fix the indentation.

Upvotes: 4

Related Questions