Rafael Mesquita
Rafael Mesquita

Reputation: 43

How to find files and extract time from filenames and store them in an array?

I have a folder with several different files in it (txt, dat, jpg) and I need to read all the files with the end "triang.dat". These files contain their time on the filename as in:

"NIK_054504_triang.dat"

I managed to find the files and convert the times into seconds:

mypath = '/home/rmesqui/Desktop/Upleg/New/'

k=0


for file in os.listdir(mypath):
     if file.endswith("triang.dat"):
         k = k+1

filenames = np.zeros(k)

print filenames
k = 0
for file in os.listdir(mypath):
    if file.endswith("triang.dat"):
        #filenames[k] = file    
        filenames[k] = 
float(file[4:6])*3600.+float(file[6:8])*60.+float(file[8:10])
        k = k+1

timearr = np.sort(filenames)-np.min(filenames)

But I have to sort filenames because the procedure to read the filenames, returns out of order files. However, I need to read these files in order, since the time of the data taking is important for the rest of the program. As in, I need to have an array such as:

lat1 = np.zeros(shape=(100+3,numberOfFiles))

where the "+3" is the time, for our example, hour = 05, minutes = 45, seconds = 04. The "100" would be the contents of a particular column in the file.

Thanks y'all!

Upvotes: 0

Views: 99

Answers (2)

Rafael Mesquita
Rafael Mesquita

Reputation: 43

I found a simple way of doing that

for file in os.listdir(mypath):
     if file.endswith("triang.dat"):
        k = k+1

filenames = np.zeros(k)

k = 0
for file in os.listdir(mypath):
    if file.endswith("triang.dat"):
        #filenames[k] = file
        filenames[k] = float(file[4:6])*3600.+float(file[6:8])*60.+float(file[8:10])
        k = k+1

Upvotes: 1

ascripter
ascripter

Reputation: 6213

Still not fully sure about where exactly the problem lies. So what about this:

result = []
for filename in os.listdir(mypath):
    if filename.endswith("triang.dat"):
        hours, minutes, seconds = int(filename[4:6]), int(filename[6:8]), int(filename[8:10])
        with open(filename, 'r') as f:
            # do whatever needed to read the content from the file
            your_100_values_read_from_the_file = range(100)
        result.append([hours, minutes, seconds] + your_100_values_read_from_the_file)

# result is now a list of lists. sort by timestamp
result.sort(key=lambda x: (x[0], x[1], x[2]))

# create your array and transpose since you want one file per column, not per row
lat1 = np.array(result).T

print (lat1.shape)   # should be (103, numberOfFiles)

Upvotes: 0

Related Questions