1

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/

Creating files and directories via Python

6
  • try os.chdir before writing to file Commented Jan 24, 2020 at 19:31
  • 1
    Three dots Path('.../ isn't a valid path. Commented Jan 24, 2020 at 19:37
  • @thebjorn, that's what I thought initially too, but I think they are just obfuscating their local filesystem with it's usernames and whatnot. I'm sorta concerned about this line 'CSV Combined'/file performing a division operator on a string? Assuming it's just a copy error. Commented Jan 24, 2020 at 19:42
  • @OsmosisJonesLoL no that's just a pathlib operation. Commented Jan 24, 2020 at 19:45
  • I see, still confused at why he would want CSV Combined at the front of his path, and then appending the rest of what's in file. I would assume open(saveTo/fileName, 'w+') was what was desired. Commented Jan 24, 2020 at 19:56

1 Answer 1

1

This worked for me - using the Path attributes and methods to construct the new file's path. It gets all the text files in the workspace and makes new files (with a '.csv' extension) in the saveto path.

import os
from pathlib import Path    

workspace = Path(os.getcwd(),'output')
saveto = Path(workspace,'CSV Combined')
#saveto.mkdir()    # if it does not exist

for p in workspace.glob('*.txt'):
    new = Path(saveto,p.name)
    new = new.with_suffix('.foo')
    #print(f'save:{p} to {new}')
    with p.open() as infile, new.open('w') as outfile:
        # process infile here
        #outfile.write(processed_infile)
        outfile.write(infile.read())

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.