Update JDCN_TXTFileSaver.py

added mode option: append, overwrite, new
This commit is contained in:
daxcay
2024-04-23 21:07:45 +05:30
parent 515d7081c3
commit fd65b967be
+20 -6
View File
@@ -1,7 +1,7 @@
import os
def save_file(filename, output_dir, content):
def save_file(filename, output_dir, content, mode='new'):
"""
Saves content to a file in the specified directory.
@@ -9,18 +9,31 @@ def save_file(filename, output_dir, content):
filename (str): The name of the file to be created.
output_dir (str): The directory where the file will be saved.
content (str): The content to be written to the file.
mode (str): The mode of saving the file. Possible options are 'append', 'overwrite', or 'new'.
Returns:
None
"""
os.makedirs(output_dir, exist_ok=True)
file_path = os.path.join(output_dir, filename)
if mode == 'new':
counter = 0
while os.path.exists(file_path):
counter += 1
file_path = os.path.join(output_dir, f"{os.path.splitext(filename)[0]}_{counter}{os.path.splitext(filename)[1]}")
elif mode == 'append' and os.path.exists(file_path):
with open(file_path, 'a') as file:
file.write(content)
print(f"Content appended successfully to {file_path}")
return
elif mode == 'overwrite' and os.path.exists(file_path):
os.remove(file_path)
with open(file_path, 'w') as file:
file.write(content)
print(f"File saved successfully at {file_path}")
class JDCN_TXTFileSaver:
def __init__(self):
@@ -32,7 +45,8 @@ class JDCN_TXTFileSaver:
"required": {
"content": ("STRING",{"forceInput": True}),
"file_name": ("STRING",{"forceInput": True}),
"directory": ("STRING", {"default": "directory path"})
"directory": ("STRING", {"default": "directory path"}),
"mode": (['append','overwrite','new'],),
},
}
@@ -41,13 +55,13 @@ class JDCN_TXTFileSaver:
FUNCTION = "SaveIT"
OUTPUT_NODE = True
def SaveIT(self, content, file_name, directory):
def SaveIT(self, content, file_name, directory, mode):
try:
if not os.path.exists(directory):
os.makedirs(directory)
save_file(file_name, directory, content)
save_file(f"{file_name}.txt", directory, content, mode)
except Exception as e:
print(f"Error saving: {e}")