import os from service.trunks_service import TrunksService class FileReader2: @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/%E5%86%85%E7%A7%91%E5%AD%A6%20%E7%AC%AC10%E7%89%88.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\\《内科学 第10版》' FileReader2.find_and_print_split_files(directory)