# Buggy code
def write_ep_file(self, ep_obj):
    """
    Generates and writes ep_obj's metadata under the given path with the
    given filename root. Uses the episode's name with the extension in
    _ep_nfo_extension.

    ep_obj: Episode object for which to create the metadata

    file_name_path: The file name to use for this metadata. Note that the extension
            will be automatically added based on _ep_nfo_extension. This should
            include an absolute path.

    Note that this method expects that _ep_data will return an ElementTree
    object. If your _ep_data returns data in another format you'll need to
    override this method.
    """

    data = self._ep_data(ep_obj)

    if not data:
        return False

    nfo_file_path = self.get_episode_file_path(ep_obj)
    nfo_file_dir = os.path.dirname(nfo_file_path)

    try:
        if not os.path.isdir(nfo_file_dir):
            log.debug(
                "Metadata directory did not exist, creating it at {location}",
                {"location": nfo_file_dir},
            )
            os.makedirs(nfo_file_dir)
            helpers.chmod_as_parent(nfo_file_dir)

        log.debug("Writing episode nfo file to {location}", {"location": nfo_file_path})

        with io.open(nfo_file_path, "wb") as nfo_file:
            # Calling encode directly, b/c often descriptions have wonky characters.
            data.write(nfo_file, encoding="utf-8", xml_declaration=True)

        helpers.chmod_as_parent(nfo_file_path)

    except IOError as e:
        log.error(
            "Unable to write file to {location} - are you sure the folder is writable? {error}",
            {"location": nfo_file_path, "error": ex(e)},
        )
        return False

    return True


# Traceback (WindowsError)
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E05
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x13 - Instant Replay.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x13 - Instant Replay.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x13 - Instant Replay.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x13 - Instant Replay.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E06
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x19 - Dog Daze Afternoon.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x19 - Dog Daze Afternoon.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x19 - Dog Daze Afternoon.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x19 - Dog Daze Afternoon.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E07
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x23 - Grandpa and the Galactic Golddigger.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x23 - Grandpa and the Galactic Golddigger.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x23 - Grandpa and the Galactic Golddigger.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x23 - Grandpa and the Galactic Golddigger.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E08
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x33 - Fugitive Fleas.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x33 - Fugitive Fleas.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x33 - Fugitive Fleas.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x33 - Fugitive Fleas.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E09
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x27 - Elroy Meets Orbitty.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x27 - Elroy Meets Orbitty.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x27 - Elroy Meets Orbitty.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x27 - Elroy Meets Orbitty.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E10
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x17 - Judy Takes Off.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x17 - Judy Takes Off.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x17 - Judy Takes Off.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x17 - Judy Takes Off.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E11
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x24 - Boy George.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x24 - Boy George.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x24 - Boy George.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x24 - Boy George.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E12
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x12 - S'no Relative.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x12 - S'no Relative.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x12 - S'no Relative.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x12 - S'no Relative.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E13
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x07 - Rip-Off Rosie.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x07 - Rip-Off Rosie.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x07 - Rip-Off Rosie.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x07 - Rip-Off Rosie.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E14
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x26 - Space Bong.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x26 - Space Bong.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x26 - Space Bong.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x26 - Space Bong.jpg exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E15
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x32 - Family Fallout.xml exists: True
2018-04-30 19:05:43 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x32 - Family Fallout.jpg exists: True
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x32 - Family Fallout.xml exists: True
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x32 - Family Fallout.jpg exists: True
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E16
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x29 - Solar Snoops.xml exists: True
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x29 - Solar Snoops.jpg exists: True
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x29 - Solar Snoops.xml exists: True
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x29 - Solar Snoops.jpg exists: True
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E17
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x14 - Haunted Halloween.xml exists: True
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x14 - Haunted Halloween.jpg exists: True
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x14 - Haunted Halloween.xml exists: True
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Checking if \\Tower\Media\TV\The Jetsons\Season 02\metadata\The Jetsons - 2x14 - Haunted Halloween.jpg exists: True
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Retrieving/creating episode S02E18
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Episode location missing: \\Tower\Media\TV\The Jetsons\Season 02\The Jetsons - 2x41 - Elroy in Wonderland.mkv
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Matched release \\Tower\Media\TV\The Jetsons\Season 02\The Jetsons - 2x41 - Elroy in Wonderland to a series in your database: The Jetsons
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Parsed \\Tower\Media\TV\The Jetsons\Season 02\The Jetsons - 2x41 - Elroy in Wonderland into title: The Jetsons, season: 2, episode: [41], episode_title: Elroy in Wonderland, type: episode, parsing_time: 0.124000072479, total_time: 0.125, absolute_episode: [], quality: Unknown
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Found codec for The Jetsons A Jetson Christmas Carol
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] 76204: Formatting pattern: %SN - S%0SE18 - %EN -> The Jetsons - S02E18 - A Jetson Christmas Carol
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Metadata provider MediaBrowser creating episode metadata for The Jetsons - S02E18 - A Jetson Christmas Carol
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Episode location missing: \\Tower\Media\TV\The Jetsons\Season 02\The Jetsons - 2x41 - Elroy in Wonderland.mkv
2018-04-30 19:05:44 DEBUG    SHOWQUEUE-REFRESH :: [77b2091] Metadata directory missing, creating it at 
2018-04-30 19:05:44 ERROR    SHOWQUEUE-REFRESH :: [77b2091] 76204: Error while refreshing show The Jetsons. Error: 
Traceback (most recent call last):
  File "C:\Medusa\Medusa\medusa\show_queue.py", line 640, in run
    self.show.write_metadata()
  File "C:\Medusa\Medusa\medusa\tv\series.py", line 856, in write_metadata
    self.__write_episode_nfos()
  File "C:\Medusa\Medusa\medusa\tv\series.py", line 885, in __write_episode_nfos
    cur_ep.create_meta_files()
  File "C:\Medusa\Medusa\medusa\tv\episode.py", line 1110, in create_meta_files
    self.__create_nfo(metadata_provider)
  File "C:\Medusa\Medusa\medusa\tv\episode.py", line 1124, in __create_nfo
    result = metadata_provider.create_episode_metadata(self) or result
  File "C:\Medusa\Medusa\medusa\metadata\generic.py", line 254, in create_episode_metadata
    return self.write_ep_file(ep_obj)
  File "C:\Medusa\Medusa\medusa\metadata\generic.py", line 469, in write_ep_file
    os.makedirs(nfo_file_dir)
  File "C:\Medusa\Medusa\medusa\init\filesystem.py", line 113, in patched_output
    return handle_output(f(*args, **kwargs))
  File "C:\Medusa\Python\lib\os.py", line 157, in makedirs
    mkdir(name, mode)
  File "C:\Medusa\Medusa\medusa\init\filesystem.py", line 113, in patched_output
    return handle_output(f(*args, **kwargs))
WindowsError: [Error 3] The system cannot find the path specified: u''

# Fixed code
def write_ep_file(self, ep_obj):
    """
    Generates and writes ep_obj's metadata under the given path with the
    given filename root. Uses the episode's name with the extension in
    _ep_nfo_extension.

    ep_obj: Episode object for which to create the metadata

    file_name_path: The file name to use for this metadata. Note that the extension
            will be automatically added based on _ep_nfo_extension. This should
            include an absolute path.

    Note that this method expects that _ep_data will return an ElementTree
    object. If your _ep_data returns data in another format you'll need to
    override this method.
    """

    data = self._ep_data(ep_obj)

    if not data:
        return False

    nfo_file_path = self.get_episode_file_path(ep_obj)
    nfo_file_dir = os.path.dirname(nfo_file_path)

    if not (nfo_file_path and nfo_file_dir):
        log.debug(
            "Unable to write episode nfo file because episode location is missing."
        )
        return False

    try:
        if not os.path.isdir(nfo_file_dir):
            log.debug(
                "Metadata directory did not exist, creating it at {location}",
                {"location": nfo_file_dir},
            )
            os.makedirs(nfo_file_dir)
            helpers.chmod_as_parent(nfo_file_dir)

        log.debug("Writing episode nfo file to {location}", {"location": nfo_file_path})

        with io.open(nfo_file_path, "wb") as nfo_file:
            # Calling encode directly, b/c often descriptions have wonky characters.
            data.write(nfo_file, encoding="utf-8", xml_declaration=True)

        helpers.chmod_as_parent(nfo_file_path)

    except IOError as e:
        log.error(
            "Unable to write file to {location} - are you sure the folder is writable? {error}",
            {"location": nfo_file_path, "error": ex(e)},
        )
        return False

    return True