# Buggy code
def disk_usage(path):
    if platform.system() == "Windows":
        import sys

        _, total, free = (
            ctypes.c_ulonglong(),
            ctypes.c_ulonglong(),
            ctypes.c_ulonglong(),
        )
        if sys.version_info >= (3,) or isinstance(path, unicode):
            method = ctypes.windll.kernel32.GetDiskFreeSpaceExW
        else:
            method = ctypes.windll.kernel32.GetDiskFreeSpaceExA
        ret = method(path, ctypes.byref(_), ctypes.byref(total), ctypes.byref(free))
        if ret == 0:
            logger.log(
                "Unable to determine free space, something went wrong", logger.WARNING
            )
            raise ctypes.WinError()
        return free.value

    elif hasattr(os, "statvfs"):  # POSIX
        import subprocess

        call = subprocess.Popen(
            ["df", path], stdout=subprocess.PIPE, stderr=subprocess.PIPE
        )
        output = call.communicate()[0]
        return output.split("\n")[1].split()[3]
    else:
        raise Exception("Unable to determine free space on your OS")


# Traceback (AATypeError)
2016-03-28 08:59:59 DEBUG    POSTPROCESSER :: 'Traceback (most recent call last):\n  File "/usr/share/sickrage/sickbeard/scheduler.py", line 106, in run\n    self.action.run(self.force)\n  File "/usr/share/sickrage/sickbeard/auto_postprocessor.py", line 56, in run\n    processTV.processDir(sickbeard.TV_DOWNLOAD_DIR, force=force)\n  File "/usr/share/sickrage/sickbeard/processTV.py", line 154, in func_wrapper\n    ret = func(*args, **kargs)\n  File "/usr/share/sickrage/sickbeard/processTV.py", line 295, in processDir\n    process_media(processPath, videoFiles, nzbName, process_method, force, is_priority, result)\n  File "/usr/share/sickrage/sickbeard/processTV.py", line 572, in process_media\n    result.result = processor.process()\n  File "/usr/share/sickrage/sickbeard/postProcessor.py", line 1040, in process\n    if not verify_freespace(self.file_path, ep_obj.show._location, [ep_obj] + ep_obj.relatedEps):  # pylint: disable=protected-access\n  File "/usr/share/sickrage/sickbeard/helpers.py", line 1617, in verify_freespace\n    diskfree += ek(os.path.getsize, f.location)\nTypeError: coercing to Unicode: need string or buffer, int found\n'
AATypeError: coercing to Unicode: need string or buffer, int found
AA    diskfree += ek(os.path.getsize, f.location)
AA  File "/usr/share/sickrage/sickbeard/helpers.py", line 1617, in verify_freespace
AA    if not verify_freespace(self.file_path, ep_obj.show._location, [ep_obj] + ep_obj.relatedEps):  # pylint: disable=protected-access
AA  File "/usr/share/sickrage/sickbeard/postProcessor.py", line 1040, in process
AA    result.result = processor.process()
AA  File "/usr/share/sickrage/sickbeard/processTV.py", line 572, in process_media
AA    process_media(processPath, videoFiles, nzbName, process_method, force, is_priority, result)
AA  File "/usr/share/sickrage/sickbeard/processTV.py", line 295, in processDir
AA    ret = func(*args, **kargs)
AA  File "/usr/share/sickrage/sickbeard/processTV.py", line 154, in func_wrapper
AA    processTV.processDir(sickbeard.TV_DOWNLOAD_DIR, force=force)
AA  File "/usr/share/sickrage/sickbeard/auto_postprocessor.py", line 56, in run
AA    self.action.run(self.force)
AA  File "/usr/share/sickrage/sickbeard/scheduler.py", line 106, in run
AATraceback (most recent call last):
2016-03-28 08:59:59 ERROR    POSTPROCESSER :: [a908b34] Exception generated in thread POSTPROCESSER: coercing to Unicode: need string or buffer, int found
2016-03-28 08:59:59 DEBUG    POSTPROCESSER :: Trying to determine free space on destination drive
2016-03-28 08:59:59 INFO     POSTPROCESSER :: This download is marked a priority download so I'm going to replace an existing file if I find one
2016-03-28 08:59:59 DEBUG    POSTPROCESSER :: File /children/Hey Arnold!/s01e06.mkv is larger than /completed/sickrage/Hey.Arnold.S01.DVDRip.x264-DEiMOS/proof-hey.arnold.s01e06.dvdrip.x264-deimos.vob
2016-03-28 08:59:59 DEBUG    POSTPROCESSER :: Is ep a priority download: True
2016-03-28 08:59:59 DEBUG    POSTPROCESSER :: This was manually downloaded but it appears to be better quality than what we have so I'm marking it as priority
2016-03-28 08:59:59 DEBUG    POSTPROCESSER :: Quality of the episode we're processing: SD DVD
2016-03-28 08:59:59 DEBUG    POSTPROCESSER :: Hey.Arnold.S01.DVDRip.x264-DEiMOS looks like it has quality SD DVD, using that
2016-03-28 08:59:59 DEBUG    POSTPROCESSER :: Looking up quality for name Hey.Arnold.S01.DVDRip.x264-DEiMOS, got SD DVD
2016-03-28 08:59:59 DEBUG    POSTPROCESSER :: Checking if /children/Hey Arnold!/s01e06-thumb.jpg exists: True
2016-03-28 08:59:59 DEBUG    POSTPROCESSER :: Checking if /children/Hey Arnold!/s01e06.nfo exists: True

# Fixed code
def disk_usage(path):
    if platform.system() == "Windows":
        import sys

        _, total, free = (
            ctypes.c_ulonglong(),
            ctypes.c_ulonglong(),
            ctypes.c_ulonglong(),
        )
        if sys.version_info >= (3,) or isinstance(path, unicode):
            method = ctypes.windll.kernel32.GetDiskFreeSpaceExW
        else:
            method = ctypes.windll.kernel32.GetDiskFreeSpaceExA
        ret = method(path, ctypes.byref(_), ctypes.byref(total), ctypes.byref(free))
        if ret == 0:
            logger.log(
                "Unable to determine free space, something went wrong", logger.WARNING
            )
            raise ctypes.WinError()
        return free.value

    elif hasattr(os, "statvfs"):  # POSIX
        import subprocess

        call = subprocess.Popen(
            ["df", "-B", "K", path], stdout=subprocess.PIPE, stderr=subprocess.PIPE
        )
        output = call.communicate()[0]
        return int(output.split("\n")[1].split()[3]) * 1024
    else:
        raise Exception("Unable to determine free space on your OS")