general_tool.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """
  2. 通用处理工具,字符串切分
  3. """
  4. from pathlib import Path
  5. def divide_string(s, num_parts):
  6. """
  7. 切割字符串为指定均分的部分
  8. :param s: 待切割的字符串
  9. :param num_parts: 切割份数
  10. :return: 切分结果
  11. """
  12. n = len(s)
  13. part_size = n // num_parts
  14. sizes = [part_size + 1 if i < n % num_parts else part_size for i in range(num_parts)]
  15. parts = []
  16. start = 0
  17. for size in sizes:
  18. parts.append(s[start:start + size])
  19. start += size
  20. return parts
  21. def find_relative_directories(root_dir, target_dir):
  22. """
  23. 查找指定目录下的目标目录相对路径
  24. :param root_dir: 根目录
  25. :param target_dir: 目标目录
  26. :return: 根目录到目标目录的相对路径
  27. """
  28. root_path = Path(root_dir)
  29. yolox_paths = []
  30. # 递归查找指定目录
  31. for path in root_path.rglob(target_dir):
  32. if path.is_dir():
  33. # 计算相对路径
  34. relative_path = path.relative_to(root_path)
  35. yolox_paths.append(relative_path)
  36. return yolox_paths