util.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import base64
  2. import time,shutil, os
  3. import logging
  4. import os
  5. from logging.handlers import TimedRotatingFileHandler
  6. from datetime import datetime
  7. from colorama import Fore, Style, init
  8. def encode_to_base64(text):
  9. encoded_bytes = base64.b64encode(text.encode('utf-8'))
  10. return encoded_bytes.decode('utf-8').replace('=', '_deng_hao_').replace('+', '_jia_hao_').replace('/', '_xie_gang_')
  11. def decode_from_base64(encoded_text):
  12. encoded_text = encoded_text.replace('_deng_hao_', '=').replace('_jia_hao_','+').replace('_xie_gang_', '/')
  13. decoded_bytes = base64.b64decode(encoded_text.encode('utf-8'))
  14. return decoded_bytes.decode('utf-8')
  15. def remove_files(basic_path):
  16. current_time = time.time()
  17. TIME_THRESHOLD_FILEPATH = 10 * 24 * 60 * 60
  18. TIME_THRESHOLD_FILE = 2 * 60
  19. for root, dirs, files in os.walk(basic_path, topdown=False):
  20. try:
  21. if current_time - os.path.getmtime(root) > TIME_THRESHOLD_FILEPATH:
  22. print(f"删除文件夹: {root}")
  23. shutil.rmtree(root)
  24. continue
  25. for file in files:
  26. file_path = os.path.join(root, file)
  27. if current_time - os.path.getmtime(file_path) > TIME_THRESHOLD_FILE:
  28. print(f"删除文件: {file_path}")
  29. os.remove(file_path)
  30. # 删除文件夹
  31. for dir in dirs:
  32. dir_path = os.path.join(root, dir)
  33. if current_time - os.path.getmtime(dir_path) > TIME_THRESHOLD_FILEPATH:
  34. print(f"删除文件夹: {dir_path}")
  35. shutil.rmtree(dir_path)
  36. except Exception as e:
  37. print(f'删除文件出错:{e}')
  38. if __name__ == "__main__":
  39. # 示例
  40. chinese_text = "分区名称ggg.pdf"
  41. encoded_text = encode_to_base64(chinese_text)
  42. print("Base64 编码:", encoded_text)
  43. decoded_text = decode_from_base64(encoded_text)
  44. print("解码后:", decoded_text)