File size: 1,062 Bytes
5c9215b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from knowledgeassistant.exception.exception import KnowledgeAssistantException
from knowledgeassistant.logging.logger import logging
import os
import sys

def write_txt_file(file_path: str, content: str, replace: bool = False):
    try:
        if replace and os.path.exists(file_path):
            os.remove(file_path)
            logging.info(f"Successfully removed the file: {file_path}")
        dir_path = os.path.dirname(file_path)
        logging.info(f"Successfully create the file: {file_path}")
        os.makedirs(dir_path, exist_ok = True)
        with open(file_path, 'w', encoding='utf-8') as txt_file:
            txt_file.write(content)
    except Exception as e:
        raise KnowledgeAssistantException(e, sys)

def read_txt_file(file_path: str) -> str:
    try:
        with open(file_path, 'r', encoding='utf-8') as txt_file:
            text = txt_file.read()
            logging.info("Successfully read the file")
            return text
    except Exception as e:
        raise KnowledgeAssistantException(e, sys)