Home > Software design >  how to open multiple .dat files to process them in a loop?
how to open multiple .dat files to process them in a loop?

Time:08-28

I am using with open () to open .dat files each time to process data. However, I would like to know how can I open multiple .dat files to process them in a loop? The goal is to process all .dat files and write the results to a single .dat file as output.

Below is the code using just a single .dat file, and a sample .dat file

5.577 #chord
14.043888 #alpha
10.275 #plane
1.00    0.000
0.98339 -0.015466667
0.96457 -0.00925
0.94365 -0.0057
0.92072 -0.00455
0.89589 -0.006016667
0.86929 -0.0106
0.84103 -0.018666667
0.81126 -0.030266667
0.78011 -0.045216667
0.74774 -0.06345
0.71431 -0.084716667
0.67996 -0.108333333
0.64487 -0.133333333
0.60921 -0.1587
0.57314 -0.183483333
0.53685 -0.206933333
0.50049 -0.228416667
0.46426 -0.247466667
0.42831 -0.2636
0.39283 -0.2763
0.35799 -0.285566667
0.32394 -0.291466667
0.29086 -0.29405
0.25891 -0.293383333
0.22823 -0.289583333
0.19897 -0.282833333
0.17128 -0.273366667
0.14528 -0.261383333
0.12111 -0.246966667
0.09887 -0.23035
0.07868 -0.211583333
0.06063 -0.190666667
0.04481 -0.167733333
0.03126 -0.143116667
0.02014 -0.116766667
0.01141 -0.088833333
0.00515 -0.0604
0.00138 -0.024316667
0.00    0.0
0.00138 0.02925
0.00515 0.069
0.01141 0.099166667
0.02014 0.1286
0.03126 0.15675
0.04481 0.18335
0.06063 0.2079
0.07868 0.230066667
0.09887 0.2497
0.12111 0.2666
0.14528 0.280733333
0.17128 0.291866667
0.19897 0.2999
0.22823 0.30485
0.25891 0.3069
0.29086 0.306166667
0.32394 0.30285
0.35799 0.297083333
0.39283 0.289116667
0.42831 0.27915
0.46426 0.267466667
0.50049 0.254366667
0.53685 0.240133333
0.57314 0.225083333
0.60921 0.209466667
0.64487 0.193516667
0.67996 0.17745
0.71431 0.16145
0.74774 0.145666667
0.78011 0.13025
0.81126 0.115333333
0.84103 0.10105
0.86929 0.087516667
0.89589 0.074816667
0.92072 0.06305
0.94365 0.052283333
0.96457 0.042383333
0.98339 0.033283333
1.00    0.000
import math


with open('Airfoils/ffaw3600.dat') as input:
    chord = float(next(input).split()[0])
    alpha = float(next(input).split()[0])
    plane = next(input).split()[0]
    mylist = [line.rstrip('\n') for line in input]

    zs = []
    ys = []
    z_trans = -0.25*chord
    y_trans = 0
    for line in mylist:
         if len(line) > 1:
                z, y = line.split(None, 2)
                zs.append(float(z))
                ys.append(float(y))
    input = tuple(zip(zs, ys))
zs = []
ys = []
for point in data:
    z, y = point
    zs.append(float(z)*chord)
    ys.append(float(y)*chord)
        
input = tuple(zip(zs, ys))

with open("Airfoils/ffaw3600new.dat", "w") as output:
    output.write(str(input)) 

CodePudding user response:

You can use something like this and write to output file each time you read from input file.

This option is better if you are going to process a lot of files (but this will work only if you can process data from files and write it sequentially).

list_of_files = ["file1.dat", "file2.dat", ...]
with open("output.dat", "w") as output:
    for file in list_of_files:
        with open(file, "r") as input:
            # read and process data
            data = ...
            output.write(str(data)   "\n")

Also you can read everything from input files and then write to output file, but this will take more of your memory if you're going to write lots of information.

list_of_files = ["file1.dat", "file2.dat", ...]
for file in list_of_files:
    with open(file, "r") as input:
        # process data
        data = ...

with open("output.dat", "w") as output:
    output.write(str(data)   "\n")
  • Related