file_reader2.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334
  1. import os
  2. from service.trunks_service import TrunksService
  3. class FileReader2:
  4. @staticmethod
  5. def find_and_print_split_files(directory):
  6. for root, dirs, files in os.walk(directory):
  7. for file in files:
  8. if '_split_' in file and file.endswith('.txt'):
  9. #if file.endswith('.md'):
  10. try:
  11. file_path = os.path.join(root, file)
  12. relative_path = '\\report\\' + os.path.relpath(file_path, directory)
  13. with open(file_path, 'r', encoding='utf-8') as f:
  14. lines = f.readlines()
  15. meta_header = lines[0]
  16. content = ''.join(lines[1:])
  17. 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'})
  18. except Exception as e:
  19. print(f'Error processing file {file_path}: {str(e)}')
  20. @staticmethod
  21. def process_txt_files(directory):
  22. for root, dirs, files in os.walk(directory):
  23. for file in files:
  24. if file.endswith('.txt'):
  25. file_path = os.path.join(root, file)
  26. with open(file_path, 'r', encoding='utf-8') as f:
  27. content = f.read()
  28. title = os.path.splitext(file)[0]
  29. TrunksService().create_trunk({'file_path': file_path, 'content': content, 'type': 'trunk', 'title': title})
  30. if __name__ == '__main__':
  31. directory = 'E:\\project\\vscode\\《内科学 第10版》'
  32. FileReader2.find_and_print_split_files(directory)