October 22, 2024
Chicago 12, Melborne City, USA
python

How to check if WindowsPath is_absolute() if path is provided in posix format (ie '/windows/')


I have a CLI that is suppose to accept absolute paths but not relative paths. I set up my promp_toolkit PathCompleter to only complete absolute paths, only to be surprised that it would allow me to use posix strings to resolve windows directories. So for example if I want something in C:\Windows I can put /windows and it will understand I’m referring to the C: and autocomplete correctly. An odd but potentially mildly useful feature.

Only problem is that when I call is_absolute() on the a WindowsPath of ‘/windows’ it returns False, despite it effectively referring to an absolute path. So now I have an autocompleter that will complete a string that my argparser then complains about, not so good.

Is there a way to determine that the posix format string is still refering to an absolute Windows path so I can manage to not barf on these sort of paths? If not I’ll have to figure out some way to keep autocompleter from suggesting them.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video