Python automating text data files into csv
Question: I am trying to automate a process where in a specific folder, there are
multiple text files following the same data format/structure. In the text
files, the data is separated by a comma. I want to be able to output all of
these text files into one cumulative csv file. This is what I currently have,
and seem to be stuck where I am because of my lack of python knowledge.

    
    
    from collections import defaultdict
    import glob
    
    def get_site_files():
        sites = defaultdict(list)
        for fname in glob.glob('*.txt'):
    
    csv_out = csv.writer(open('out.csv', 'w'), delimiter=',')
    
    f = open('myfile.txt')
    for line in f:
      vals = line.split(',')
      csv_out.writerow()
    f.close()
    

EDIT: bringing up comments: I want to make sure that all of the text files are
read, not just only myfile.txt.

Also, if I could combine them all into one large .txt file and then I could
make those into a csv that would be great too, I just am not sure the exact
way to do this.


Answer: Just a little bit of reordering of your code.

    
    
    import csv
    import glob
    
    def get_site_files():
        with open('out.csv', 'w') as out_file:
            csv_out = csv.writer(out_file, delimiter=',')
            for fname in glob.glob('*.txt'):
                with open(fname) as f:
                    for line in f:
                        vals = line.split(',')
                        csv_out.writerow(vals)
    
    get_site_files()
    

But since they are all in the same format you can just concatenate them:

    
    
    import glob
    with ('out.csv', 'w') as fout:
        for fname in glob.glob('*.txt'):
            with open(fname, 'r') as fin:
                fout.write(fin.read())
    

