Search code examples
pythonpython-3.xfor-loopfile-ioglob

How to Run script for multiple text files with a common string?


In my research work I have multiple text files has a common string "Max", and Max has different values in range of 0.10 to 2.00 with step 0.10 as follow:

 A_100Hz_Rate20Hz_5tot_0.10Max_1_ 
 A_100Hz_Rate20Hz_5tot_0.10Max_2_
 A_100Hz_Rate20Hz_5tot_0.10Max_3_
 .
 .
 .
 A_100Hz_Rate20Hz_5tot_2.00Max_1_ 
 A_100Hz_Rate20Hz_5tot_2.00Max_2_
 A_100Hz_Rate20Hz_5tot_2.00Max_3_

I need to import all files depending on the value of Max (ex: 0.10Max) to get the average of files with the same Max values separately to get:

 Ave_A_100Hz_Rate20Hz_5tot_0.10Max_3_
 .
 .
 .
 Ave_A_100Hz_Rate20Hz_5tot_2.00Max_3_

I’ve tried a manual glob module, and it's working good for one value of "Max" but for the full range it doeson't work. This is my code:

import numpy as np
import glob
import pandas as pd

h = np.linspace(0.10,2.00,20)  
for x in h: 
     x1 = ("%.2f" % x)
     glob_path = 'input/*_{}Vbr_*.txt'.format(x1)
     import_files = glob.glob(glob_path)
     print(x,import_files )
     for index, file_name in enumerate(import_files ):
          merged_data = pd.read_csv(file_name, header=None, delimiter="\t").values
          if index==0:    
               summation = merged_data
          else:
               summation = summation + merged_data
          averaging = summation/len(import_files)         
          np.savetxt('output/Ave_'+file_name[10:], averaging, delimiter="\t" )

I need to write a general script. But, in my case now I used the script with just two values x = 1.50 and x = 2.0 to make it simple. I tried print(import_files) and expected the output to be:

['input\\A_100Hz_Rate20Hz_5tot_1.50Max_1_.txt', 
'input\\A_100Hz_Rate20Hz_5tot_1.50Max_2_.txt', 
'input\\A_100Hz_Rate20Hz_5tot_1.50Max_3_.txt']


['input\\A_100Hz_Rate20Hz_5tot_2.00Max_1_.txt', 
'input\\A_100Hz_Rate20Hz_5tot_2.00Max_2_.txt', 
'input\\A_100Hz_Rate20Hz_5tot_2.00Max_3_.txt']

But the actual output is (in short):

0.1 []
0.2 []
1.5 ['input\\A_100Hz_Rate20Hz_5tot_1.50Max_1_.txt', 
'input\\A_100Hz_Rate20Hz_5tot_1.50Max_2_.txt', 
'input\\A_100Hz_Rate20Hz_5tot_1.50Max_3_.txt']
1.6 []
1.7 []
2.0['input\\A_100Hz_Rate20Hz_5tot_2.00Max_1_.txt', 
'input\\A_100Hz_Rate20Hz_5tot_2.00Max_2_.txt', 
'input\\A_100Hz_Rate20Hz_5tot_2.00Max_3_.txt']

and it caused an error in the kernel

      np.savetxt('output/Ave_'+file_name[10:], averaging, delimiter="\t" )

 NameError: name 'file_name' is not defined

Please, Any suggestions?


Solution

  • I think that you have just to test whether import_file is empty:

    for x in h: 
         x1 = ("%.2f" % x)
         glob_path = 'input/*_{}Vbr_*.txt'.format(x1)
         import_files = glob.glob(glob_path)
         print(x,import_files )
         if len(import_files) != 0:
             for index, file_name in enumerate(import_files ):
                  merged_data = pd.read_csv(file_name, header=None, delimiter="\t").values
                  if index==0:    
                       summation = merged_data
                  else:
                       summation = summation + merged_data
              averaging = summation/len(import_files)         
              np.savetxt('output/Ave_'+file_name[10:], averaging, delimiter="\t" )