path.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # Copyright © 2024 Noah Vogt <noah@noahvogt.com>
  2. # This program is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. from os import path
  13. from .log import error_msg
  14. def expand_dir(directory: str) -> str:
  15. expanded_user_dir = path.expanduser(directory)
  16. expanded_user_and_env_vars_dir = path.expandvars(expanded_user_dir)
  17. abs_path = path.abspath(expanded_user_and_env_vars_dir)
  18. return abs_path
  19. def make_sure_file_exists(filename: str) -> None:
  20. if not path.isfile(filename):
  21. try:
  22. with open(filename, mode="w+", encoding="utf-8") as file_creator:
  23. file_creator.write("")
  24. except (FileNotFoundError, PermissionError, IOError) as error:
  25. error_msg(
  26. "Failed to create file in '{}'. Reason: {}".format(
  27. filename, error
  28. )
  29. )