From b0b0464fdcf88c45fc5dd452eb0e4bc7976f5f4f Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sun, 5 Jan 2020 23:28:53 -0800 Subject: [PATCH] Add winglob.py. --- voussoirkit/winglob.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 voussoirkit/winglob.py diff --git a/voussoirkit/winglob.py b/voussoirkit/winglob.py new file mode 100644 index 0000000..c62b6ef --- /dev/null +++ b/voussoirkit/winglob.py @@ -0,0 +1,17 @@ +''' +On Windows, square brackets do not have a special meaning in glob strings. +However, python's glob module is written for unix-style globs in which brackets +represent character classes / ranges. + +Calling glob.escape would also escape asterisk which may not be desired. +So this module just provides a modified version of glob.glob which will escape +only square brackets when called on windows, and behave normally on linux. +''' +import glob as python_glob +import os +import re + +def glob(pattern): + if os.name == 'nt': + pattern = re.sub(r'(\[|\])', r'[\1]', pattern) + return python_glob.glob(pattern)