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.
This commit is contained in:
Geeknasty
2024-08-15 23:24:11 -04:00
committed by GitHub
parent e7db7fc8f4
commit 9a830cd836
+6
View File
@@ -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: