The mimetypes Module
The mimetypes module contains support for determining the MIME type for a given uniform resource locator. This is based on a built-in table, plus Apache and Netscape configuration files, if they are found. This module is demonstrated in Example 6-9.
Example 6-9. Using the mimetypes Module
File: mimetypes-example-1.py
import mimetypes
import glob, urllib
for file in glob.glob("samples/*"):
url = urllib.pathname2url(file)
print file, mimetypes.guess_type(url)
samplessample.au ('audio/basic', None)
samplessample.ini (None, None)
samplessample.jpg ('image/jpeg', None)
samplessample.msg (None, None)
samplessample.tar ('application/x-tar', None)
samplessample.tgz ('application/x-tar', 'gzip')
samplessample.txt ('text/plain', None)
samplessample.wav ('audio/x-wav', None)
samplessample.zip ('application/zip', None)
Категории