fileio.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # -*- coding:utf-8 -*-
  2. """
  3. File I/O utilities for DDNS with Python 2/3 compatibility
  4. @author: NewFuture
  5. """
  6. import os
  7. from io import open # Python 2/3 compatible UTF-8 file operations
  8. def _ensure_directory_exists(file_path): # type: (str) -> None
  9. """
  10. Internal helper to ensure directory exists for the given file path
  11. Args:
  12. file_path (str): File path whose directory should be created
  13. Raises:
  14. OSError: If directory cannot be created
  15. """
  16. directory = os.path.dirname(file_path)
  17. if directory and not os.path.exists(directory):
  18. os.makedirs(directory)
  19. def read_file_safely(file_path, encoding="utf-8", default=None): # type: (str, str, str|None) -> str
  20. """
  21. Safely read file content with UTF-8 encoding, return None if file doesn't exist or can't be read
  22. Args:
  23. file_path (str): Path to the file to read
  24. encoding (str): File encoding (default: utf-8)
  25. Returns:
  26. str | None: File content or None if failed
  27. """
  28. try:
  29. return read_file(file_path, encoding)
  30. except Exception:
  31. return default # type: ignore
  32. def write_file_safely(file_path, content, encoding="utf-8"): # type: (str, str, str) -> bool
  33. """
  34. Safely write content to file with UTF-8 encoding
  35. Args:
  36. file_path (str): Path to the file to write
  37. content (str): Content to write
  38. encoding (str): File encoding (default: utf-8)
  39. Returns:
  40. bool: True if write successful, False otherwise
  41. """
  42. try:
  43. write_file(file_path, content, encoding)
  44. return True
  45. except Exception:
  46. return False
  47. def read_file(file_path, encoding="utf-8"): # type: (str, str) -> str
  48. """
  49. Read file content with UTF-8 encoding, raise exception if failed
  50. Args:
  51. file_path (str): Path to the file to read
  52. encoding (str): File encoding (default: utf-8)
  53. Returns:
  54. str: File content
  55. Raises:
  56. IOError: If file cannot be read
  57. UnicodeDecodeError: If file cannot be decoded with specified encoding
  58. """
  59. with open(file_path, "r", encoding=encoding) as f:
  60. return f.read()
  61. def write_file(file_path, content, encoding="utf-8"): # type: (str, str, str) -> None
  62. """
  63. Write content to file with UTF-8 encoding, raise exception if failed
  64. Args:
  65. file_path (str): Path to the file to write
  66. content (str): Content to write
  67. encoding (str): File encoding (default: utf-8)
  68. Raises:
  69. IOError: If file cannot be written
  70. UnicodeEncodeError: If content cannot be encoded with specified encoding
  71. """
  72. _ensure_directory_exists(file_path)
  73. with open(file_path, "w", encoding=encoding) as f:
  74. f.write(content)
  75. def ensure_directory(file_path): # type: (str) -> bool
  76. """
  77. Ensure the directory for the given file path exists
  78. Args:
  79. file_path (str): File path whose directory should be created
  80. Returns:
  81. bool: True if directory exists or was created successfully
  82. """
  83. try:
  84. _ensure_directory_exists(file_path)
  85. return True
  86. except (OSError, IOError):
  87. return False