I have multiple .txt
files inside folder/subfolders as shown below:
- States (folder)
- Arizona (subfoler)
- file1.txt
- file2.txt
- file3.txt
- Alaska (subfolder)
- file1.txt
- file2.txt
- file3.txt
- file4.txt
- Arkansas (subfolder)
- file1.txt
- file2.txt
- Arizona (subfoler)
I need to convert all the files to csv and combine csv files per folder (such as arizona_files.csv, alaska_files.csv). I tried to use the code below and there was no output. Any idea what I am doing wrong?
import os
import csv
for root, dirnames, filenames in os.walk(path):
for filename in filenames:
if filename.endswith('.txt'):
txt_file = ('*.txt')
csv_file = ('*.csv')
in_txt = csv.reader(open(filename, "rb"), delimiter = '\t')
out_csv = csv.writer(open('*.csv', 'wb'))
out_csv.writerows(filename)
os.path.join(root, filename)
to get the path to the file. Also, the code looks wrong in so many ways. '*.csv' is an awkward name. Also, you are overwriting the content of that file for each file you read in. – lightalchemist