thombashi / pathvalidate

A Python library to sanitize/validate a string such as filenames/file-paths/etc.
https://pathvalidate.rtfd.io/
MIT License
220 stars 13 forks source link

ignoring the platforms os.sep, and always using r"/" #12

Closed bschollnick closed 4 years ago

bschollnick commented 4 years ago

As far as I can tell under the Windows platform, the following issues exist:

These are for sanitize_filepath:

  1. The filepath can not start with os.sep (eg. "/users/benjamin/test.zip")

    from pathvalidate import sanitize_filepath sanitize_filepath(r"users\benjamin\test\test1.zip") 'users/benjamin/test/test1.zip' sanitize_filepath(r"\users\benjamin\test\test1.zip") Traceback (most recent call last): File "", line 1, in File "C:\Users\bschollnick.URMC-SH\python3\av1\lib\site-packages\pathvalidate_filepath.py", line 376, in sanitize_filepath ).sanitize(file_path, replacement_text) File "C:\Users\bschollnick.URMC-SH\python3\av1\lib\site-packages\pathvalidate_filepath.py", line 74, in sanitize self.__fpath_validator.validate_abspath(value) File "C:\Users\bschollnick.URMC-SH\python3\av1\lib\site-packages\pathvalidate_filepath.py", line 206, in validate_abspath raise err_object pathvalidate.error.ValidationError: reason=MALFORMED_ABS_PATH, target-platform=universal, description=an invalid absolute file path (\users\benjamin\test\test1.zip) for the platform (universal). to avoid the error, specify an appropriate platform correspond with the path format, or 'auto'.

import os print(os.sep) '\'

sanitize_filepath(r"users\benjamin\test\test1.zip") 'users/benjamin/test/test1.zip'

Please note, the string I passed in was using os.sep. What was returned, did not use os.sep.

thombashi commented 4 years ago

@bschollnick Thank you for your report.

The filepath can not start with os.sep (eg. "/users/benjamin/test.zip")

This issue had fixed at pathvalidate 2.2.2:

>>> from pathvalidate import sanitize_filepath
>>> sanitize_filepath(r"\users\benjamin\test\test1.zip")
'/users/benjamin/test/test1.zip'

sanitize_filepath does not seem to respect the os.sep setting of the platform it is running on:

You can get your expected results if you specify the platform argument to "windows" or "auto" to sanitie_filepath.

>>> sanitize_filepath(r"users\benjamin\test\test1.zip", platform="windows")
'users\\benjamin\\test\\test1.zip'
>>> sanitize_filepath(r"users\benjamin\test\test1.zip", platform="auto")
'users\\benjamin\\test\\test1.zip'

In default, sanitize_filepath normalize a file path to be platform-independent.