Save files to a new folder with python

129 Views Asked by At

This code saves text files from a data frame of sentences, then saves each one as a ssml file.

How can I get the sentences to be saved in a new folder?

max = len(sentences) 
for i in range(0,max):
    txt = sentences[i]
    new_txt = starter + txt + ender   
    print(new_txt)
    num = num + 1 
    with open("text" + str(num) + ".ssml", 'w+') as f:
        f.writelines(new_txt) 
1

There are 1 best solutions below

0
Crapicus On

Add this at the start:

import os

folder_name = 'my_folder'
os.makedirs(folder_name, exist_ok=True)

Then change:

with open("text" + str(num) + ".ssml", 'w+') as f:

to:

with open(f'{folder_name}\\text{num}.ssml', 'w+') as f: