<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def download_to_directory(self, directory, url, basename=None, overwrite=False, subdir=None):
 """ Download a file to the workspace. Early Shortcut: If url is a file://-URL and that file is already in the directory, keep it there. If basename is not given but subdir is, assume user knows what she's doing and use last URL segment as the basename. If basename is not given and no subdir is given, use the alnum characters in the URL as the basename. Args: directory (string):
 Directory to download files to basename (string, None):
 basename part of the filename on disk. url (string):
 URL to download from overwrite (boolean):
 Whether to overwrite existing files with that name subdir (string, None):
 Subdirectory to create within the directory. Think fileGrp. Returns: Local filename """

        log = getLogger('ocrd.resolver.download_to_directory') # pylint: disable=redefined-outer-name
        log.debug("directory=|%s| url=|%s| basename=|%s| overwrite=|%s| subdir=|%s|", directory, url, basename, overwrite, subdir)

        if url is None:
            raise Exception("'url' must be a string")
        if directory is None:
            raise Exception("'directory' must be a string")

        if basename is None:
            if (subdir is not None) or \
                (directory and url.startswith('file://%s' % directory)): # in case downloading a url 'file:///tmp/foo/bar' to directory '/tmp/foo'
                basename = url.rsplit('/', 1)[-1]
            else:
                basename = safe_filename(url)

        if subdir is not None:
            basename = join(subdir, basename)

        outfilename = join(directory, basename)

        if exists(outfilename) and not overwrite:
            log.debug("File already exists and overwrite=False: %s", outfilename)
            return outfilename

        outfiledir = outfilename.rsplit('/', 1)[0]
        #  print(outfiledir)
        if not isdir(outfiledir):
            makedirs(outfiledir)

        log.debug("Downloading <%s> to '%s'", url, outfilename)

        # de-scheme file:// URL
        if url.startswith('file://'):
            url = url[len('file://'):]

        # Copy files or download remote assets
        if '://' not in url:
            copyfile(url, outfilename)
        else:
            response = requests.get(url)
            if response.status_code != 200:
                raise Exception("Not found: %s (HTTP %d)" % (url, response.status_code))
            with open(outfilename, 'wb') as outfile:
                outfile.write(response.content)

        return outfilename