splitfile.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import os
  2. from utils.fileio import *
  3. def split_file(file_dir, output_dir, endpoint=".py"):
  4. for file_path in walk_files(file_dir, endpoint):
  5. with open(file_path, 'r') as file:
  6. content = file.read()
  7. s_index = 0
  8. e_index = len(content)
  9. file_id = 1
  10. while s_index < e_index:
  11. try:
  12. left_index = content.index("def ", s_index, e_index)
  13. s_index = left_index + 1
  14. right_index = content.index("def ", s_index, e_index)
  15. content_tmp = content[left_index:right_index]
  16. if left_index > 3 and content[left_index - 4: left_index] == " ":
  17. content_tmp = content_tmp.replace("\n ", "\n")
  18. new_file_name = file_path.replace(file_dir, output_dir).replace(".py", "_" + str(file_id) + ".py")
  19. if not os.path.exists(new_file_name[:new_file_name.rindex("/")]):
  20. os.makedirs(new_file_name[:new_file_name.rindex("/")])
  21. if content[left_index + 4] != '_':
  22. with open(new_file_name, 'w') as file_out:
  23. file_out.write(content_tmp)
  24. file_id += 1
  25. except ValueError:
  26. break
  27. if s_index < e_index and content[s_index - 1: s_index + 3] == "def ":
  28. content_tmp = content[s_index - 1:e_index]
  29. if s_index > 4 and content[s_index - 5: s_index - 1] == " ":
  30. content_tmp = content_tmp.replace("\n ", "\n")
  31. new_file_name = file_path.replace(file_dir, output_dir).replace(".py", "_" + str(file_id) + ".py")
  32. if not os.path.exists(new_file_name[:new_file_name.rindex("/")]):
  33. os.makedirs(new_file_name[:new_file_name.rindex("/")])
  34. if content[s_index + 3] != '_':
  35. with open(new_file_name, 'w') as file_out:
  36. file_out.write(content_tmp)
  37. b = "asdfasdfasdf"
  38. if __name__ == '__main__':
  39. split_file("/Users/liufan/program/PYTHON/sap2nd/GnnForPrivacyScan/data/purposeCombined", "../data/purposeSplit3")