user9264558
user9264558

Reputation:

Convert multiple text file to csv using os.walk

I have multiple .txt files inside folder/subfolders as shown below:

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)

Upvotes: 0

Views: 1113

Answers (2)

Benoit Lacherez
Benoit Lacherez

Reputation: 516

As stated in https://docs.python.org/3/library/os.html the filenames provided by os.walk() contain no path elements and "To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name)." that's why you get this error.

Upvotes: 1

Logan
Logan

Reputation: 316

You are not executing the code in the correct directory. When you initialize the code in your command prompt you either need to have your python script at the top level of your iterative path. I.e. in the States folder or just above it and initiate it from that path. Or alternatively you could change your in_text to do the following:

in_txt = csv.reader(open(os.path.join(path,filename), "rb"), delimiter = '\t')

Which will tell csv.reader exactly where to find the current file. When writing the csv you would also have to add the same type of operation as well.

out_csv.writerows(os.path.join(path,filename))

Upvotes: 0

Related Questions