Python- Import Multiple Files to a single .csv file
Question: I have 125 data files containing two columns and 21 rows of data and I'd like
to import them into a single .csv file (as 125 pairs of columns and only 21
rows). This is what my data files look like:

![enter image description here](http://i.stack.imgur.com/BAXMY.png)

I am fairly new to python but I have come up with the following code:

    
    
    import glob
    Results = glob.glob('./*.data')
    fout='c:/Results/res.csv'
    fout=open ("res.csv", 'w')
     for file in Results:
     g = open( file, "r" )
     fout.write(g.read())
     g.close() 
    fout.close()
    

The problem with the above code is that all the data are copied into only two
columns with 125*21 rows.

Any help is very much appreciated!


Answer: This should work:

    
    
    import glob
    
    files = [open(f) for f in glob.glob('./*.data')] #Make list of open files
    fout = open("res.csv", 'w')
    
    for row in range(21):
        for f in files:
            fout.write( f.readline().strip() ) # strip removes trailing newline
            fout.write(',')
        fout.write('\n')
    
    fout.close()
    

Note that this method will probably fail if you try a large number of files, I
believe the default limit in Python is 256.

