# Buggy code
def add_torrents_from_dir(self, torrents_dir, recursive=False):
    # TODO: Optimize this properly!!!!
    torrents_list = []
    errors_list = []

    if recursive:

        def rec_gen():
            for root, _, filenames in os.walk(torrents_dir):
                for fn in filenames:
                    yield os.path.join(root, fn)

        filename_generator = rec_gen()
    else:
        filename_generator = os.listdir(torrents_dir)

    # Build list of .torrents to process
    for f in filename_generator:
        filepath = os.path.join(torrents_dir, f)
        filename = (
            str(filepath) if sys.platform == "win32" else filepath.decode("utf-8")
        )
        if os.path.isfile(filepath) and filename.endswith(".torrent"):
            torrents_list.append(filepath)

    for chunk in chunks(
        torrents_list, 100
    ):  # 100 is a reasonable chunk size for commits
        for f in chunk:
            try:
                self.add_torrent_to_channel(TorrentDef.load(f))
            except DuplicateTorrentFileError:
                pass
            except:
                errors_list.append(f)
        orm.commit()  # Kinda optimization to drop excess cache?

    return torrents_list, errors_list


# Traceback (RuntimeError)
Traceback (most recent call last):
File "TriblerGUI\core_manager.py", line 118, in on_received_state
RuntimeError: [Failure instance: Traceback: <type 'exceptions.ValueError'>: The filename, directory name, or volume label syntax is incorrect
run_tribler.py:85:start_tribler
Tribler\Core\Session.py:432:start
lib\site-packages\twisted\internet\defer.py:321:addCallback
lib\site-packages\twisted\internet\defer.py:310:addCallbacks
--- <exception caught here> ---
lib\site-packages\twisted\internet\defer.py:653:_runCallbacks
Tribler\Core\Session.py:429:load_checkpoint
Tribler\Core\Session.py:384:load_checkpoint
Tribler\Core\APIImplementation\LaunchManyCore.py:570:load_checkpoint
Tribler\Core\APIImplementation\LaunchManyCore.py:567:do_load_checkpoint
Tribler\Core\APIImplementation\LaunchManyCore.py:599:resume_download
Tribler\Core\TorrentDef.py:115:load_from_dict
Tribler\Core\TorrentDef.py:68:__init__
]

# Fixed code
def add_torrents_from_dir(self, torrents_dir, recursive=False):
    # TODO: Optimize this properly!!!!
    torrents_list = []
    errors_list = []

    if recursive:

        def rec_gen():
            for root, _, filenames in os.walk(torrents_dir):
                for fn in filenames:
                    yield os.path.join(root, fn)

        filename_generator = rec_gen()
    else:
        filename_generator = os.listdir(torrents_dir)

    # Build list of .torrents to process
    for f in filename_generator:
        filepath = ensure_unicode(
            os.path.join(
                ensure_unicode(torrents_dir, "utf-8"), ensure_unicode(f, "utf-8")
            ),
            "utf-8",
        )
        if os.path.isfile(filepath) and ensure_unicode(f, "utf-8").endswith(".torrent"):
            torrents_list.append(filepath)

    for chunk in chunks(
        torrents_list, 100
    ):  # 100 is a reasonable chunk size for commits
        for f in chunk:
            try:
                self.add_torrent_to_channel(TorrentDef.load(f))
            except DuplicateTorrentFileError:
                pass
            except:
                errors_list.append(f)
        orm.commit()  # Kinda optimization to drop excess cache?

    return torrents_list, errors_list