file_utils.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import json
  2. import os
  3. import sys
  4. import re
  5. from os import path
  6. from typing import Dict, List
  7. abspath = os.path.abspath(__file__)
  8. root_path = re.sub(r"poyee-data-warehouse.*", "poyee-data-warehouse", abspath)
  9. sys.path.append(root_path)
  10. from dw_base import NORM_GRN, NORM_RED
  11. from dw_base import PROJECT_ROOT_PATH
  12. from dw_base.utils.log_utils import pretty_print
  13. def append_file(content: str, file_path: str):
  14. """
  15. 将字符串写入文件(追加写入)
  16. Args:
  17. content: 字符串内容
  18. file_path: 文件路径
  19. Returns:
  20. """
  21. with open(file_path, "a", encoding='utf-8') as file:
  22. file.write(content)
  23. def delete_file(file_path: str):
  24. """
  25. 删除文件
  26. Args:
  27. file_path: 文件路径
  28. Returns:
  29. """
  30. os.system(f'rm -f {file_path}')
  31. def get_abs_path(fs_path: str, check_exist: bool = True) -> str:
  32. """
  33. 获取文件的绝对路径,注意传入的路径必须是``项目``目录下的相对路径或绝对路径(多此一举)
  34. Args:
  35. fs_path: 文件的路径
  36. check_exist: 是否对文件存在情况进行判定, 一般情况下, 读文件为True, 写文件为False
  37. Returns:
  38. 文件的绝对路径
  39. """
  40. if fs_path.startswith(PROJECT_ROOT_PATH):
  41. if not check_exist or os.path.exists(fs_path):
  42. return fs_path
  43. raise FileNotFoundError('绝对路径 %s 不存在' % fs_path)
  44. abs_path = None
  45. for sub_dir in os.listdir(PROJECT_ROOT_PATH):
  46. if fs_path.startswith(sub_dir):
  47. abs_path = os.path.join(PROJECT_ROOT_PATH, fs_path)
  48. break
  49. if abs_path is None:
  50. raise FileNotFoundError('相对路径 `%s` 不存在' % fs_path)
  51. if not check_exist or os.path.exists(abs_path):
  52. return abs_path
  53. raise FileNotFoundError('绝对路径 %s 不存在' % abs_path)
  54. def list_files(fs_dir: str, recursive: bool = False, extension: str = None) -> List[str]:
  55. """
  56. 列出给定目录下所有文件
  57. Args:
  58. fs_dir: 目录(绝对路径或项目下的相对路径)
  59. recursive: 是否递归
  60. extension: 文件扩展名
  61. Returns:
  62. 所有子文件(绝对路径)
  63. """
  64. fs_dir = get_abs_path(fs_dir, check_exist=True)
  65. files_with_abspath = []
  66. for child in os.listdir(fs_dir):
  67. abs_child = f'{fs_dir}/{child}'
  68. if extension is not None and not abs_child.endswith(extension):
  69. continue
  70. if os.path.isfile(abs_child):
  71. files_with_abspath.append(abs_child)
  72. elif recursive:
  73. for sub_child in list_files(abs_child, recursive):
  74. files_with_abspath.append(path.abspath(sub_child))
  75. return files_with_abspath
  76. def load_json_file(file_path: str) -> Dict[str, any]:
  77. """
  78. 读取文件,返回JSON
  79. Args:
  80. file_path: 文件路径
  81. Returns:
  82. """
  83. try:
  84. with open(get_abs_path(file_path), 'r') as file:
  85. return json.load(file)
  86. except Exception as e:
  87. pretty_print(f'{NORM_RED}加载json文件 {NORM_GRN}{file_path}{NORM_RED} 失败:{str(e)}')
  88. raise e
  89. def read_file_content(file_path: str) -> str:
  90. """
  91. 读取文件内容,作为一个字符串返回
  92. Args:
  93. file_path: 文件路径
  94. Returns: 文件内容
  95. """
  96. with open(get_abs_path(file_path), 'r') as file:
  97. return file.read()
  98. def read_file_lines(file_path: str) -> List[str]:
  99. """
  100. 读取文件内容,作为多行字符串返回(每行依然保留有换行符)
  101. Args:
  102. file_path: 文件路径
  103. Returns:
  104. """
  105. with open(get_abs_path(file_path), 'r', encoding='utf-8') as file:
  106. return file.readlines()
  107. def write_file(content: str, file_path: str):
  108. """
  109. 将字符串写入文件(覆盖写入)
  110. Args:
  111. content: 字符串内容
  112. file_path: 文件路径
  113. Returns:
  114. """
  115. with open(file_path, "w", encoding='utf-8') as file:
  116. file.write(content)
  117. if __name__ == '__main__':
  118. ol = list_files('launch-pad/contacts/00new_contact/03fix/04mid_pre', False)
  119. for e in ol:
  120. print(e)
  121. print('-----------------------------------------------------')
  122. for e in sorted(ol):
  123. print(e)