I'm trying to write a Python script to batch convert all tab separated .txt files in a source directory to comma separated .csv files, but retaining the original file names in the output.
I'm fairly new to this, but at the moment my script can create the new .csv files. However, an empty row has been added in between every data filled row in my output files. How can I resolve this problem?
import csv
import os
source_path = r"file location"
dest_path = r"file location"
for file in os.listdir(source_path):
# Get filename without file extension
filename_no_extension = os.path.splitext(file)[0]
# Concatenate filename amd paths
dest_csv_file = str(filename_no_extension) + ".csv"
dest_file = os.path.join(dest_path,dest_csv_file)
source_file = os.path.join(source_path,file)
# Open the original file and create a reader object
with open(source_file, "r") as infile:
reader = csv.reader(infile, dialect="excel-tab")
with open(dest_file, "w") as outfile:
writer = csv.writer(outfile, delimiter = ',')
for row in reader:
writer.writerow(row)