If you do not need CSV in memory, just copying from input to output, it will be much cheaper to avoid parsing altogether and copy without accumulating in memory:
import shutil #import csv files from folder path = r'data/US/market/merged_data' allFiles = glob.glob(path + "/*.csv") with open('someoutputfile.csv', 'wb') as outfile: for i, fname in enumerate(allFiles): with open(fname, 'rb') as infile: if i != 0: infile.readline() # Throw away header on all but first file # Block copy rest of file from input to output without parsing shutil.copyfileobj(infile, outfile) print(fname + " has been imported.")
It; shutil.copyfileobj efficiently copies data, significantly reducing work at the Python level for analysis and reinitialization.
It is assumed that all CSV files have the same format, encoding, line endings, etc., and the header does not contain embedded newline characters, but in this case it is much faster than alternatives.
source share