import os from service.trunks_service import TrunksService class FileReader: @staticmethod def find_and_print_split_files(directory): for root, dirs, files in os.walk(directory): for file in files: if '_split_' in file and file.endswith('.txt'): #if file.endswith('.md'): try: file_path = os.path.join(root, file) relative_path = '\\report\\' + os.path.relpath(file_path, directory) with open(file_path, 'r', encoding='utf-8') as f: lines = f.readlines() meta_header = lines[0] content = ''.join(lines[1:]) TrunksService().create_trunk({'file_path': relative_path, 'content': content,'type':'trunk','meta_header':meta_header,'referrence':'http://173.18.12.205:8001/books/%E6%80%A5%E8%AF%8A%E5%8C%BB%E5%AD%A6%EF%BC%88%E7%AC%AC2%E7%89%88%EF%BC%89.pdf'}) except Exception as e: print(f'Error processing file {file_path}: {str(e)}') @staticmethod def process_txt_files(directory): for root, dirs, files in os.walk(directory): for file in files: if file.endswith('.txt'): file_path = os.path.join(root, file) with open(file_path, 'r', encoding='utf-8') as f: content = f.read() title = os.path.splitext(file)[0] TrunksService().create_trunk({'file_path': file_path, 'content': content, 'type': 'trunk', 'title': title}) if __name__ == '__main__': directory = 'E:\\project\\vscode\\《急诊医学(第2版)》' FileReader.find_and_print_split_files(directory)