So I'm trying to convert a ton of txt files to csv and save them in a different folder. I can covert them no problem, but they always save in the same folder they are read from. I've tried joinpath, os.path.join, folder+filename, open (file 'w+' and 'x' and 'w'). The print statements in the covertToCSV function always give me the right folder and then show that the file doesn't get made in that folder.
...\nlps\11-CSV converted\CSV Combined
...\nlps\11-CSV converted\CSV Combined
...\nlps\11-CSV converted\Admission_Consult.csv
No matter what I try, I can't get it to save to the folder I want. This is getting hilarious. Links I've read through are at the bottom.
import sys
import csv
from pathlib import Path
workspace = Path('.../nlps/11-CSV converted')
saveTo = Path('.../nlps/11-CSV converted/CSV Combined')
def openFiles(dir):
filePaths = list(workspace.glob('*.txt'))
return filePaths
# Converts given file to CSV with one column with tabs as delimiter
def convertToCSV(filePaths):
for fileName in filePaths:
with open(fileName, 'r') as in_file:
stripped = (line.strip() for line in in_file)
lines = (line.split("\t") for line in stripped if line)
fileName = fileName.with_suffix('.csv')
newFile = workspace.joinpath('CSV Combined')
file = newFile.joinpath(fileName)
print(saveTo)
print(newFile)
print(file)
with open('CSV Combined'/file, 'w+') as out_file:
writer = csv.writer(out_file)
writer.writerows(lines)
https://docs.python.org/3/library/pathlib.html
https://docs.python.org/3/library/os.html#os.chmod
https://docs.python.org/3/library/functions.html#open
https://docs.python.org/3.8/library/csv.html
Writing to a new directory in Python without changing directory
How to write file in a different directory in python?
https://thispointer.com/how-to-create-a-directory-in-python/