
from __future__ import absolute_import
from __future__ import division

import os
import re


magic_check = re.compile('([*?[])')
magic_check_bytes = re.compile(b'([*?[])')


def escape(pathname):
    """Escape all special characters.
    """
    drive, pathname = os.path.splitdrive(pathname)
    if isinstance(pathname, bytes):
        pathname = magic_check_bytes.sub(br'[\1]', pathname)
    else:
        pathname = magic_check.sub(r'[\1]', pathname)
    return drive + pathname
