Search code examples
pythonglob

Extracting certain columns from multiple files simultaneously by Python


My purpose is to extract one certain column from the multiple data files. So, I tried to use glob module to read files and tried to extract one column from each file with for statements like below:

filin = diri + '*_7.txt'
FileList=sorted(glob.glob(filin))
for INPUT in FileList:
    a = []
    b = []
    c = []
    T = []
    f = open(INPUT,'r')
    f.seek(0,0)
    for columns in ( raw.strip().split() for raw in f):
            b.append(columns[11])
            t = np.array(b, float)
            print t
            t = list(t)
            T = T + [t]
    f.close()
print T

The number of data files which I used is 32. So, I expected the second 'for' statement ran only 32 times while generating only 32 arrays of t. However, the result doesn't look like what I expected. I assume that it may be due to the influence from the first 'for' statement, but I am not sure. Any idea or help would be really appreciated. Thank you, Isaac


Solution

  • You clear T = [] for every file. Move T = [] line before first loop.