Skip to content Skip to sidebar Skip to footer

Python: How To Save *.dat-files As *.csv-files To New Folder

I have a folder with lots of *.dat files (which were created with the program IDL). I am able to take one single file, convert it to a *.csv file and save it in a different (alread

Solution 1:

import csv
import idlsave
from os import listdir
from os.path import isfile, join, splitext

dat_folder = "/folder/to/dat/files/"
csv_folder = "/folder/to/save/new/csv/files/"

onlyfilenames = [f for f in listdir(dat_folder) if isfile(join(dat_folder,f))]
for fullfilename in onlyfilenames:
    file_name, file_extension = splitext(fullfilename)
    if file_extension == ".dat":
        input_file = idlsave.read(dat_folder + fullfilename)
        n = input_file["raw"]
        with open(join(csv_folder, file_name + ".csv"), "w", newline='') as f:
            writer = csv.writer(f)
            writer.writerows(n)

Post a Comment for "Python: How To Save *.dat-files As *.csv-files To New Folder"