Here are 10 Python snippets demonstrating how to determine MIME types of files based on their extensions using the mimetypes library. Each snippet is separated by a delimiter.
import mimetypes
file_path = 'unknown.xyz'
mime_type, encoding = mimetypes.guess_type(file_path)
if mime_type is None:
print(f"The MIME type for '{file_path}' is unknown.")
else:
print(f"MIME type: {mime_type}")
import mimetypes
file_path = 'unknownfile.abc'
mime_type, encoding = mimetypes.guess_type(file_path)
if mime_type:
print(f"File: {file_path}, MIME type: {mime_type}")
else:
print(f"No MIME type could be determined for {file_path}.")