detect_file_type method

detect_file_type

Detects the file type of the container item.

def detect_file_type(self, detection_mode):
    ...
Parameter Type Description
detection_mode FileTypeDetectionMode Defines a mode of the file type detection.

Returns: An instance of FileType; FileType.unknown if a file type isn’t detected.

The detection_mode parameter provides the ability to control file type detection:

  • Default – The file type is detected by the file extension; if the file extension isn’t recognized, the file type is detected by the file content.
  • Extension – The file type is detected only by the file extension.
  • Content – The file type is detected only by the file content.

Example

from groupdocs.parser import Parser
from groupdocs.parser.options import FileTypeDetectionMode

def detect_items(file_path):
    with Parser(file_path) as parser:
        attachments = parser.get_container()
        if not attachments:
            print("Container extraction isn't supported")
            return
        for item in attachments:
            file_type = item.detect_file_type(FileTypeDetectionMode.DEFAULT)
            print(f"{item.name}: {file_type}")

See Also