diff --git a/pathvalidate/_filename.py b/pathvalidate/_filename.py index d5c8f53..1e9655e 100644 --- a/pathvalidate/_filename.py +++ b/pathvalidate/_filename.py @@ -78,6 +78,8 @@ def sanitize(self, value: PathType, replacement_text: str = "") -> PathType: if self.platform in [Platform.UNIVERSAL, Platform.WINDOWS]: # Do not end a file or directory name with a space or a period sanitized_filename = sanitized_filename.rstrip(" .") + # Do not start a file or directory name with a space + sanitized_filename = sanitized_filename.lstrip(" ") elif e.reason == ErrorReason.NULL_NAME: return self._null_value_handler(e) else: @@ -209,6 +211,15 @@ def __validate_win_filename(self, unicode_filename: str) -> None: description="Do not end a file or directory name with a space or a period", ) + if unicode_filename[0] in (" "): + raise InvalidCharError( + self._ERROR_MSG_TEMPLATE.format( + invalid=re.escape(unicode_filename[0]), value=repr(unicode_filename) + ), + platform=Platform.WINDOWS, + description="Do not start a file or directory name with a space", + ) + def validate_filename( filename: PathType, diff --git a/test/test_filename.py b/test/test_filename.py index cbdbdca..152acd9 100644 --- a/test/test_filename.py +++ b/test/test_filename.py @@ -541,8 +541,11 @@ def test_normal_check_reserved(self, value, check_reserved, expected): [ ["windows", "period.", "period"], ["windows", "space ", "space"], + ["windows", " space ", "space"], ["windows", "space_and_period .", "space_and_period"], ["windows", "space_and_period. ", "space_and_period"], + ["windows", " .space_and_period", ".space_and_period"], + ["windows", ". space_and_period", ". space_and_period"], ["linux", "period.", "period."], ["linux", "space ", "space "], ["linux", "space_and_period. ", "space_and_period. "],