From 9a830cd8367a6b050e1077c4e87cdb540c580fbf Mon Sep 17 00:00:00 2001 From: Geeknasty <103335300+Geeknasty@users.noreply.github.com> Date: Thu, 15 Aug 2024 23:24:11 -0400 Subject: [PATCH] feat: Expand environment variables in file paths for DirectoryFileCounter and TextFileLineCounter This update enhances the `DirectoryFileCounter` and `TextFileLineCounter` classes by expanding environment variables in file paths using `os.path.expandvars()`. This change allows these classes to correctly handle paths that include environment variables, improving their flexibility and usability in various environments. - **DirectoryFileCounter**: The `execute_file_counter` method now expands environment variables in the `directory_path` before performing directory operations. - **TextFileLineCounter**: The `execute_line_counter` method now expands environment variables in the `file_path` before reading the file. This fix ensures that paths containing variables such as `%userprofile%` are properly resolved, enabling more dynamic and environment-aware file handling. --- line_counter.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/line_counter.py b/line_counter.py index 7cbf044..56119a4 100644 --- a/line_counter.py +++ b/line_counter.py @@ -18,6 +18,9 @@ class DirectoryFileCounter(): CATEGORY = "CCTech/Files" def execute_file_counter(self, directory_path: str) -> dict: + # Expand environment variables in the directory path + directory_path = os.path.expandvars(directory_path) + if not os.path.isdir(directory_path): raise FileNotFoundError(f"The path '{directory_path}' does not exist.") @@ -49,6 +52,9 @@ class TextFileLineCounter(): CATEGORY = "CCTech/Files" def execute_line_counter(self, file_path: str) -> dict: + # Expand environment variables in the file path + file_path = os.path.expandvars(file_path) + if not os.path.isfile(file_path): raise FileNotFoundError(f"The file at path '{file_path}' does not exist.") with open(file_path, 'r', encoding='utf-8') as file: