mimetypes --- ファイル名を MIME タイプへマップする¶
ソースコード: Lib/mimetypes.py
mimetypes モジュールは、ファイル名あるいは URL と、ファイル名拡張子に関連付けられた MIME 型とを変換します。ファイル名から MIME 型へと、 MIME 型からファイル名拡張子への変換が提供されます; 後者の変換では符号化方式はサポートされていません。
このモジュールは、一つのクラスと多くの便利な関数を提供します。これらの関数がこのモジュールへの標準のインターフェースですが、アプリケーションによっては、そのクラスにも関係するかもしれません。
以下で説明されている関数は、このモジュールへの主要なインターフェースを提供します。たとえモジュールが初期化されていなくても、もしこれらの関数が、 init() がセットアップする情報に依存していれば、これらの関数は、 init() を呼びます。
- mimetypes.guess_type(url, strict=True)¶
url で与えられるファイル名あるいは URL に基づいて、ファイルの型を推定します。URL は文字列または path-like object です。
戻り値は、タプル
(type, encoding)です、ここで type は、もし型が(拡張子がないあるいは未定義のため)推定できない場合は、Noneを、あるいは、 MIME content-type ヘッダ に利用できる、'type/subtype'の形の文字列です。encoding は、符合化方式がない場合は
Noneを、あるいは、符号化に使われるプログラムの名前 (たとえば、 compress あるいは gzip)です。符号化方式は Content-Encoding ヘッダとして使うのに適しており、 Content-Transfer-Encoding ヘッダには適して いません 。マッピングはテーブル駆動です。符号化方式のサフィックスは大/小文字を区別します; データ型サフィックスは、最初大/小文字を区別して試し、それから大/小文字を区別せずに試します。The optional strict argument is a flag specifying whether the list of known MIME types is limited to only the official types registered with IANA. When strict is
True(the default), only the IANA types are supported; when strict isFalse, some additional non-standard but commonly used MIME types are also recognized.バージョン 3.8 で変更: Added support for url being a path-like object.
Soft deprecated since version 3.13: Passing a file path instead of URL is soft deprecated. Use
guess_file_type()for this.
- mimetypes.guess_file_type(path, *, strict=True)¶
Guess the type of a file based on its path, given by path. Similar to the
guess_type()function, but accepts a path instead of URL. Path can be a string, a bytes object or a path-like object.Added in version 3.13.
- mimetypes.guess_all_extensions(type, strict=True)¶
Guess the extensions for a file based on its MIME type, given by type. The return value is a list of strings giving all possible filename extensions, including the leading dot (
'.'). The extensions are not guaranteed to have been associated with any particular data stream, but would be mapped to the MIME type type byguess_type()andguess_file_type().省略可能な strict 引数は
guess_type()関数のものと同じ意味を持ちます。
- mimetypes.guess_extension(type, strict=True)¶
Guess the extension for a file based on its MIME type, given by type. The return value is a string giving a filename extension, including the leading dot (
'.'). The extension is not guaranteed to have been associated with any particular data stream, but would be mapped to the MIME type type byguess_type()andguess_file_type(). If no extension can be guessed for type,Noneis returned.省略可能な strict 引数は
guess_type()関数のものと同じ意味を持ちます。
モジュールの動作を制御するために、いくつかの追加の関数とデータ項目が利用できます。
- mimetypes.init(files=None)¶
内部のデータ構造を初期化します。もし files が与えられていれば、これはデフォルトの type map を増やすために使われる、一連のファイル名でなければなりません。もし省略されていれば、使われるファイル名は
knownfilesから取られます。 Windows であれば、現在のレジストリの設定が読み込まれます。 files あるいはknownfiles内の各ファイル名は、それ以前に現れる名前より優先されます。繰り返しinit()を呼び出すことは許されています。files に空リストを与えることで、システムのデフォルトが適用されるのを避けることが出来ます; 組み込みのリストから well-known な値だけが取り込まれます。
files が
Noneの場合、内部のデータ構造は初期のデフォルト値に完全に再構築されます。 これは安定な操作であり、複数回呼び出されたときは同じ結果になります。バージョン 3.2 で変更: 前のバージョンでは、 Windows のレジストリの設定は無視されていました。
- mimetypes.read_mime_types(file)¶
Load the type map given in the file named by file, if it exists. file must be a string specifying the name of the file to read. The type map is returned as a dictionary mapping file extensions, including the leading dot (
'.'), to strings of the form'type/subtype'. If the file does not exist or cannot be read,Noneis returned.
- mimetypes.add_type(type, ext, strict=True)¶
MIME 型 type からのマッピングを拡張子 ext に追加します。拡張子がすでに既知であれば、新しい型が古いものに置き替わります。その型がすでに既知であれば、その拡張子が、既知の拡張子のリストに追加されます。
strict が
Trueの時(デフォルト)は、そのマッピングは正式な MIME 型に、そうでなければ、非標準の MIME 型に追加されます。
- mimetypes.knownfiles¶
共通にインストールされた型マップファイル名のリスト。これらのファイルは、普通
mime.typesという名前であり、パッケージごとに異なる場所にインストールされます。
- mimetypes.suffix_map¶
サフィックスをサフィックスにマップする辞書。これは、符号化方式と型が同一拡張子で示される符号化ファイルが認識できるように使用されます。例えば、
.tgz拡張子は、符号化と型が別個に認識できるように.tar.gzにマップされます。
- mimetypes.encodings_map¶
ファイル名拡張子を符号化方式型にマッピングする辞書。
- mimetypes.types_map¶
ファイル名拡張子を MIME 型にマップする辞書。
- mimetypes.common_types¶
ファイル名拡張子を非標準ではあるが、一般に使われている MIME 型にマップする辞書。
モジュールの使用例:
>>> import mimetypes
>>> mimetypes.init()
>>> mimetypes.knownfiles
['/etc/mime.types', '/etc/httpd/mime.types', ... ]
>>> mimetypes.suffix_map['.tgz']
'.tar.gz'
>>> mimetypes.encodings_map['.gz']
'gzip'
>>> mimetypes.types_map['.tgz']
'application/x-tar-gz'
MimeTypes Objects¶
MimeTypes クラスは一つ以上の MIME 型データベースが欲しいアプリケーションにとって有用でしょう。これは mimetypes モジュールのそれと似たインターフェースを提供します。
- class mimetypes.MimeTypes(filenames=(), strict=True)¶
This class represents a MIME-types database. By default, it provides access to the same database as the rest of this module. The initial database is created from Python's built-in MIME type tables. It may be extended by loading additional
mime.types-style files into the database using theread()orreadfp()methods. The mapping dictionaries may also be cleared before loading additional data if the default data is not desired.省略可能な filenames パラメータは、追加のファイルを、デフォルトデータベースの"トップに"ロードさせるのに使うことができます。
- suffix_map¶
Dictionary mapping suffixes to suffixes. This is used to allow recognition of encoded files for which the encoding and the type are indicated by the same extension. For example, the
.tgzextension is mapped to.tar.gzto allow the encoding and type to be recognized separately. This is initialized with some predefined values.
- encodings_map¶
Dictionary mapping filename extensions to encoding types. This is initialized with some predefined values.
- types_map¶
Tuple containing two dictionaries, mapping filename extensions to MIME types: the first dictionary is for the non-standards types and the second one is for the standard types. They are initialized with some predefined values and MIME type information loaded from files specified by the filenames argument.
- types_map_inv¶
Tuple containing two dictionaries, mapping MIME types to a list of filename extensions: the first dictionary is for the non-standards types and the second one is for the standard types. They are initialized with some predefined values and MIME type information loaded from files specified by the filenames argument.
- guess_extension(type, strict=True)¶
guess_extension()関数と同様ですが、オブジェクトに保存されたテーブルを使用します。
- guess_type(url, strict=True)¶
guess_type()関数と同様ですが、オブジェクトに保存されたテーブルを使用します。
- guess_file_type(path, *, strict=True)¶
Similar to the
guess_file_type()function, using the tables stored as part of the object.Added in version 3.13.
- guess_all_extensions(type, strict=True)¶
guess_all_extensions()と同様ですが、オブジェクトに保存されたテーブルを参照します。
- read(filename, strict=True)¶
MIME 情報を、 filename という名のファイルからロードします。これはファイルを解析するのに
readfp()を使用します。strict が
Trueの時 (デフォルト) は、そのマッピングは標準 MIME 型のリストに、そうでなければ、非標準 MIME 型のリストに追加されます。
- readfp(fp, strict=True)¶
MIME 型情報を、オープンしたファイル fp からロードします。ファイルは、標準の
mime.typesファイルの形式でなければなりません。strict が
Trueの時 (デフォルト) は、そのマッピングは標準 MIME 型のリストに、そうでなければ、非標準 MIME 型のリストに追加されます。
- read_windows_registry(strict=True)¶
MIME type 情報を Windows のレジストリから読み込みます。
Availability: Windows.
strict が
Trueの時 (デフォルト) は、そのマッピングは標準 MIME 型のリストに、そうでなければ、非標準 MIME 型のリストに追加されます。Added in version 3.2.
- add_type(type, ext, strict=True)¶
MIME 型 type からのマッピングを拡張子 ext に追加します。拡張子がすでに既知であれば、新しい型が古いものに置き替わります。その型がすでに既知であれば、その拡張子が、既知の拡張子のリストに追加されます。
strict が
Trueの時(デフォルト)は、そのマッピングは正式な MIME 型に、そうでなければ、非標準の MIME 型に追加されます。