code: def guess_filetype(filepath):
      
      # If we have python-magic, use it to determine file type
      typ = None
  
      # Some specific checks for known file suffixes
      _fn = filepath.lower()
      _name, _ext = os.path.splitext(_fn)
      if _ext in known_types:
          typ = known_types[_ext]
  
      if typ is None and have_magic:
          try:
              # it seems there are conflicting versions of a 'magic'
              # module for python floating around...*sigh*
              if hasattr(magic, 'from_file'):
                  typ = magic.from_file(filepath, mime=True)
  
              elif magic_tester is not None:
                  descrip = magic_tester.file(filepath)
                  if descrip.startswith("FITS image data"):
                      return ('image', 'fits')
  
          except Exception as e:
              pass
  
      if typ is None:
          # if no magic, or magic fails, fall back to mimetypes
          try:
              typ, enc = mimetypes.guess_type(filepath)
  
          except Exception as e:
              # fail
              pass
  
      if typ:
          typ, subtyp = typ.split('/')
          return (typ, subtyp)
  
      raise ValueError("Can't determine file type of '%s'" % (filepath))
docs: Guess the type of a file.