简体   繁体   中英

Can't save files to different dir in python

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

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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM