{"id":863,"library":"filetype","title":"Filetype Inference Library","description":"Small and dependency-free Python package to infer file type and MIME type by checking the magic numbers signature of a file or buffer. It is a Python port from the 'filetype' Go package, offering a simple and friendly API for a wide range of file types. Currently at version 1.2.0, it is actively maintained with periodic updates.","status":"active","version":"1.2.0","language":"python","source_language":"en","source_url":"https://github.com/h2non/filetype.py","tags":["file","mime","type inference","magic numbers","utility","dependency-free"],"install":[{"cmd":"pip install filetype","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"filetype","correct":"import filetype"}],"quickstart":{"code":"import filetype\nimport os\n\n# Create a dummy JPEG file for demonstration\ndummy_jpeg_path = 'dummy.jpg'\n# A minimal JPEG header (first few bytes of a typical JPEG file)\n# In a real scenario, you'd read from an actual file.\njpeg_magic_bytes = b'\\xFF\\xD8\\xFF\\xE0\\x00\\x10\\x4A\\x46\\x49\\x46\\x00\\x01'\n\ntry:\n    with open(dummy_jpeg_path, 'wb') as f:\n        f.write(jpeg_magic_bytes)\n\n    # Guess the file type from the path\n    kind = filetype.guess(dummy_jpeg_path)\n\n    if kind is None:\n        print('Cannot guess file type!')\n    else:\n        print(f'File extension: {kind.extension}')\n        print(f'File MIME type: {kind.mime}')\n\n    # You can also guess from a buffer/bytes object\n    kind_from_buffer = filetype.guess(jpeg_magic_bytes)\n    if kind_from_buffer:\n        print(f'Buffer extension: {kind_from_buffer.extension}')\n        print(f'Buffer MIME type: {kind_from_buffer.mime}')\n\nfinally:\n    # Clean up the dummy file\n    if os.path.exists(dummy_jpeg_path):\n        os.remove(dummy_jpeg_path)","lang":"python","description":"This quickstart demonstrates how to use `filetype.guess()` to infer the type of a file based on its path and from a bytes buffer. It creates a dummy JPEG file to showcase the functionality and then cleans it up. The library returns a `Kind` object with `extension` and `mime` attributes, or `None` if the type cannot be determined."},"warnings":[{"fix":"Implement a check for `if kind is None:` or similar logic to handle unknown file types gracefully.","message":"The `filetype.guess()` function returns `None` if it cannot determine the type of the given file or buffer. Always check for `None` before accessing `extension` or `mime` attributes.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use `try-except FileNotFoundError` blocks to handle missing files, or verify file existence using `os.path.exists()` before calling `guess()`.","message":"`filetype.guess()` will raise a `FileNotFoundError` if the provided file path does not exist. Ensure the file path is correct and the file is accessible.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure that the buffer passed to `filetype.guess()` contains at least the initial bytes of the file, ideally up to 261 bytes, for reliable type detection.","message":"When guessing from a buffer, `filetype` typically only requires the first 261 bytes (maximum file header size) for accurate detection. Providing an insufficient number of bytes may lead to incorrect type inference or `None` being returned.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-06-09T21:17:19.786Z","next_check":"2026-06-27T00:00:00.000Z","problems":[{"fix":"You need to install the package using pip: `pip install filetype`","cause":"The 'filetype' package is not installed in your Python environment.","error":"ModuleNotFoundError: No module named 'filetype'"},{"fix":"Ensure the file is valid and supported. If reading from a buffer, ensure enough bytes are provided. Handle the `None` return gracefully: \n```python\nimport filetype\n\nkind = filetype.guess('path/to/your/file')\nif kind is None:\n    print('File type could not be determined or is not supported.')\nelse:\n    print(f'File extension: {kind.extension}')\n    print(f'File MIME type: {kind.mime}')\n```","cause":"The 'filetype' library could not infer the file type from its magic numbers. This often happens if the file is corrupted, empty, or its type is not supported by the library (version 1.2.0).","error":"filetype.guess() returns None"},{"fix":"Verify that the file path is correct and that the file exists at the given location. Use an absolute path or ensure the script is run from the correct directory. \n```python\nimport filetype\nimport os\n\nfile_path = 'path/to/correct/file.jpg'\nif os.path.exists(file_path):\n    kind = filetype.guess(file_path)\n    if kind:\n        print(f'Detected type: {kind.mime}')\n    else:\n        print('File type not recognized.')\nelse:\n    print(f'Error: File not found at {file_path}')\n```","cause":"The specified file path for `filetype.guess()` or file operations does not point to an existing file.","error":"FileNotFoundError: [Errno 2] No such file or directory: 'your_file_name'"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.2.0","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/h2non/filetype.py","docs":null,"changelog":null,"pypi":"https://pypi.org/project/filetype/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization"],"base_url":null,"auth_type":null,"install_checks":{"last_tested":"2026-06-09","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","installed_version":"1.2.0","pypi_latest":"1.2.0","is_stale":false,"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"filetype","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.02,"mem_mb":1.3,"disk_size":"18.0M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"filetype","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":1.3,"disk_size":"18.0M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"filetype","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.5,"import_time_s":0.01,"mem_mb":1.3,"disk_size":"18M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"filetype","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":1.3,"disk_size":"18M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"filetype","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.03,"mem_mb":1.3,"disk_size":"19.9M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"filetype","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.03,"mem_mb":1.3,"disk_size":"19.9M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"filetype","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.6,"import_time_s":0.02,"mem_mb":1.3,"disk_size":"20M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"filetype","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":1.3,"disk_size":"20M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"filetype","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.03,"mem_mb":1.1,"disk_size":"11.7M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"filetype","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.03,"mem_mb":1.1,"disk_size":"11.7M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"filetype","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.4,"import_time_s":0.03,"mem_mb":1.1,"disk_size":"12M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"filetype","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.03,"mem_mb":1.1,"disk_size":"12M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"filetype","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.02,"mem_mb":1.1,"disk_size":"11.5M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"filetype","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":1.1,"disk_size":"11.4M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"filetype","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.4,"import_time_s":0.02,"mem_mb":0.9,"disk_size":"12M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"filetype","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":0.9,"disk_size":"12M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"filetype","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.02,"mem_mb":1.3,"disk_size":"17.5M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"filetype","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":1.3,"disk_size":"17.5M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"filetype","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.8,"import_time_s":0.02,"mem_mb":1.3,"disk_size":"18M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"filetype","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":1.3,"disk_size":"18M"}]},"quickstart_checks":{"last_tested":"2026-04-24","tag":null,"tag_description":null,"results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]},"_links":{"self":"https://checklist.day/api/registry/filetype","v1":"https://checklist.day/v1/registry/filetype","v1_install":"https://checklist.day/v1/registry/filetype/install","v1_imports":"https://checklist.day/v1/registry/filetype/imports","v1_compatibility":"https://checklist.day/v1/registry/filetype/compatibility","v1_quickstart":"https://checklist.day/v1/registry/filetype/quickstart","docs":"https://checklist.day/docs"}}