file_reader.py 1.4 KB

12345678910111213141516171819202122232425262728293031
  1. import os
  2. from service.trunks_service import TrunksService
  3. class FileReader:
  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. file_path = os.path.join(root, file)
  11. relative_path = '\\report\\' + os.path.relpath(file_path, directory)
  12. with open(file_path, 'r', encoding='utf-8') as f:
  13. lines = f.readlines()
  14. meta_header = lines[0]
  15. content = ''.join(lines[1:])
  16. TrunksService().create_trunk({'file_path': relative_path, 'content': content,'type':'community_report'})
  17. @staticmethod
  18. def process_txt_files(directory):
  19. for root, dirs, files in os.walk(directory):
  20. for file in files:
  21. if file.endswith('.txt'):
  22. file_path = os.path.join(root, file)
  23. with open(file_path, 'r', encoding='utf-8') as f:
  24. content = f.read()
  25. title = os.path.splitext(file)[0]
  26. TrunksService().create_trunk({'file_path': file_path, 'content': content, 'type': 'mr', 'title': title})
  27. if __name__ == '__main__':
  28. directory = '/Users/ycw/work/心肌梗死病历模版'
  29. FileReader.process_txt_files(directory)