"os.path" --- 一般的なパス名操作
********************************

**ソースコード:** Lib/genericpath.py, Lib/posixpath.py (POSIX) 、
Lib/ntpath.py (Windows) 。

======================================================================

このモジュールはパス名を扱う際に便利な関数を実装しています。ファイルの
読み書きをする際は "open()" を、ファイルシステムにアクセスするには
"os" モジュールをご参照ください。パスの引数は文字列、バイト列、あるい
は "os.PathLike" プロトコルを実装するあらゆるオブジェクトとして渡すこ
とができます。

Unix シェルとは異なり、Python はあらゆるパス展開を *自動的には* 行いま
せん。アプリケーションがシェルのようなパス展開を必要とした場合は、
"expanduser()" や "expandvars()" といった関数を明示的に呼び出すことで
行えます。("glob" モジュールも参照してください)

参考: "pathlib" モジュールは高水準のパスオブジェクトを提供します。

注釈:

  以下のすべての関数は、そのパラメータにバイト列のみ、あるいは文字列の
  み受け付けます。パスまたはファイル名を返す場合、返り値は同じ型のオブ
  ジェクトになります。

注釈:

  OS によって異なるパス名の決まりがあるため、標準ライブラリにはこのモ
  ジュールのいくつかのバージョンが含まれています。 "os.path" モジュー
  ルは常に現在 Python が動作している OS に適したパスモジュールであるた
  め、ローカルのパスを扱うのに適しています。各々のモジュールをインポー
  トして *常に* 一つのフォーマットを利用することも可能です。これらはす
  べて同じインターフェースを持っています:

  * UNIX 形式のパスを扱う "posixpath"

  * Windows のパスを扱う "ntpath"

バージョン 3.8 で変更: "exists()"、 "lexists()"、 "isdir()"、
"isfile()"、 "islink()"、および "ismount()" は、OS レベルで表現できな
い文字列を含む可能性がある例外を送出する代わりに "False" を返すように
なりました。

os.path.abspath(path)

   Return a normalized absolutized version of the pathname *path*. On
   most platforms, this is equivalent to calling
   "normpath(join(os.getcwd(), path))".

   参考: "os.path.join()" and "os.path.normpath()".

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

os.path.basename(path, /)

   パス名 *path* の末尾のファイル名部分を返します。これは関数
   "split()" に *path* を渡した時に返されるペアの 2 番めの要素です。こ
   の関数が返すのは Unix の **basename** とは異なります; Unix の
   **basename** は "'/foo/bar/'" に対して "'bar'" を返しますが、関数
   "basename()" は空文字列 ("''") を返します。

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

os.path.commonpath(paths)

   Return the longest common sub-path of each pathname in the iterable
   *paths*.  Raise "ValueError" if *paths* contain both absolute and
   relative pathnames, if *paths* are on different drives, or if
   *paths* is empty.  Unlike "commonprefix()", this returns a valid
   path.

   Added in version 3.5.

   バージョン 3.6 で変更: *path-like objects* のシーケンスを受け入れる
   ようになりました。

   バージョン 3.13 で変更: Any iterable can now be passed, rather than
   just sequences.

os.path.commonprefix(list, /)

   *list* 内のすべてのパスに共通する接頭辞のうち、最も長いものを (パス
   名の 1 文字 1 文字を判断して) 返します。*list* が空の場合、空文字列
   ("''") を返します。

   注釈:

     この関数は一度に 1 文字ずつ処理するため、不正なパスを返す場合があ
     ります。有効なパスを取得するためには、"commonpath()" を参照してく
     ださい。

        >>> os.path.commonprefix(['/usr/lib', '/usr/local/lib'])
        '/usr/l'

        >>> os.path.commonpath(['/usr/lib', '/usr/local/lib'])
        '/usr'

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

os.path.dirname(path, /)

   パス名 *path* のディレクトリ名を返します。これは関数 "split()" に
   *path* を渡した時に返されるペアの 1 番めの要素です。

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

os.path.exists(path)

   *path* が実在するパスかオープンしているファイル記述子を参照している
   場合 "True" を返します。壊れたシンボリックリンクについては "False"
   を返します。一部のプラットフォームでは、たとえ *path* が物理的に存
   在していたとしても、要求されたファイルに対する "os.stat()" の実行権
   がなければこの関数が "False" を返すことがあります。

   バージョン 3.3 で変更: *path* は整数でも可能になりました: それがオ
   ープンしているファイル記述子なら "True" が返り、それ以外なら
   "False" が返ります。

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

os.path.lexists(path)

   Return "True" if *path* refers to an existing path, including
   broken symbolic links.   Equivalent to "exists()" on platforms
   lacking "os.lstat()".

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

os.path.expanduser(path)

   Unix および Windows では、与えられた引数の先頭のパス要素 "~" 、また
   は "~user" を、 *user* のホームディレクトリのパスに置き換えて返しま
   す。

   Unix では、先頭の "~" は、環境変数 "HOME" が設定されているならその
   値に置き換えられます。設定されていない場合は、現在のユーザのホーム
   ディレクトリをビルトインモジュール "pwd" を使ってパスワードディレク
   トリから探して置き換えます。先頭の "~user" については、直接パスワー
   ドディレクトリから探します。

   On Windows, "USERPROFILE" will be used if set, otherwise a
   combination of "HOMEPATH" and "HOMEDRIVE" will be used.  An initial
   "~user" is handled by checking that the last directory component of
   the current user's home directory matches "USERNAME", and replacing
   it if so.

   置き換えに失敗したり、引数のパスがチルダで始まっていなかった場合は
   、パスをそのまま返します。

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

   バージョン 3.8 で変更: Windowsで  "HOME" は参照しなくなりました。

os.path.expandvars(path)

   引数のパスの環境変数を展開して返します。引数の中の "$name" または
   "${name}" のような形式の文字列は環境変数、 *name* の値に置き換えら
   れます。不正な変数名や存在しない変数名の場合には変換されず、そのま
   ま返します。

   Windows では、 "$name" や "${name}" の形式に加えて、 "%name%" の形
   式もサポートされています。

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

os.path.getatime(path, /)

   Return the time of last access of *path*.  The return value is a
   floating-point number giving the number of seconds since the epoch
   (see the  "time" module).  Raise "OSError" if the file does not
   exist or is inaccessible.

os.path.getmtime(path, /)

   Return the time of last modification of *path*.  The return value
   is a floating-point number giving the number of seconds since the
   epoch (see the  "time" module). Raise "OSError" if the file does
   not exist or is inaccessible.

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

os.path.getctime(path, /)

   システムの ctime、Unix系など一部のシステムでは最後にメタデータが変
   更された時刻、Windows などその他のシステムでは *path* の作成時刻を
   返します。返り値はエポック ("time" モジュールを参照) からの経過時間
   を示す秒数になります。ファイルが存在しない、あるいはアクセスできな
   かった場合は "OSError" を送出します。

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

os.path.getsize(path, /)

   *path* のサイズをバイト数で返します。ファイルが存在しない、あるいは
   アクセスできなかった場合は "OSError" を送出します。

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

os.path.isabs(path, /)

   Return "True" if *path* is an absolute pathname.  On Unix, that
   means it begins with a slash, on Windows that it begins with two
   (back)slashes, or a drive letter, colon, and (back)slash together.

   参考: "abspath()"

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

   バージョン 3.13 で変更: On Windows, returns "False" if the given
   path starts with exactly one (back)slash.

os.path.isfile(path)

   *path* が "存在する" 一般ファイルなら "True" を返します。 この関数
   はシンボリックリンクの先を辿るので、同じパスに対して "islink()" と
   "isfile()" の両方が真を返すことがあります。

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

os.path.isdir(path, /)

   *path* が "存在する" ディレクトリなら "True" を返します。 この関数
   はシンボリックリンクの先を辿るので、同じパスに対して "islink()" と
   "isdir()" の両方が真を返すことがあります。

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

os.path.isjunction(path)

   Return "True" if *path* refers to an "existing" directory entry
   that is a junction.  Always return "False" if junctions are not
   supported on the current platform.

   Added in version 3.12.

os.path.islink(path)

   *path* が "存在する" ディレクトリを指すシンボリックリンクなら
   "True" を返します。 Python ランタイムがシンボリックリンクをサポート
   していないプラットフォームでは、常に "False" を返します。

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

os.path.ismount(path)

   Return "True" if pathname *path* is a *mount point*: a point in a
   file system where a different file system has been mounted.  On
   POSIX, the function checks whether *path*'s parent, "*path*/..", is
   on a different device than *path*, or whether "*path*/.." and
   *path* point to the same i-node on the same device --- this should
   detect mount points for all Unix and POSIX variants.  It is not
   able to reliably detect bind mounts on the same filesystem. On
   Linux systems, it will always return "True" for btrfs subvolumes,
   even if they aren't mount points. On Windows, a drive letter root
   and a share UNC are always mount points, and for any other path
   "GetVolumePathName" is called to see if it is different from the
   input path.

   バージョン 3.4 で変更: Added support for detecting non-root mount
   points on Windows.

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

os.path.isdevdrive(path)

   Return "True" if pathname *path* is located on a Windows Dev Drive.
   A Dev Drive is optimized for developer scenarios, and offers faster
   performance for reading and writing files. It is recommended for
   use for source code, temporary build directories, package caches,
   and other IO-intensive operations.

   May raise an error for an invalid path, for example, one without a
   recognizable drive, but returns "False" on platforms that do not
   support Dev Drives. See the Windows documentation for information
   on enabling and creating Dev Drives.

   Added in version 3.12.

   バージョン 3.13 で変更: The function is now available on all
   platforms, and will always return "False" on those that have no
   support for Dev Drives

os.path.isreserved(path)

   Return "True" if *path* is a reserved pathname on the current
   system.

   On Windows, reserved filenames include those that end with a space
   or dot; those that contain colons (i.e. file streams such as
   "name:stream"), wildcard characters (i.e. "'*?"<>'"), pipe, or
   ASCII control characters; as well as DOS device names such as
   "NUL", "CON", "CONIN$", "CONOUT$", "AUX", "PRN", "COM1", and
   "LPT1".

   注釈:

     This function approximates rules for reserved paths on most
     Windows systems. These rules change over time in various Windows
     releases. This function may be updated in future Python releases
     as changes to the rules become broadly available.

   Availability: Windows.

   Added in version 3.13.

os.path.join(path, /, *paths)

   Join one or more path segments intelligently.  The return value is
   the concatenation of *path* and all members of **paths*, with
   exactly one directory separator following each non-empty part,
   except the last. That is, the result will only end in a separator
   if the last part is either empty or ends in a separator.

   If a segment is an absolute path (which on Windows requires both a
   drive and a root), then all previous segments are ignored and
   joining continues from the absolute path segment. On Linux, for
   example:

      >>> os.path.join('/home/foo', 'bar')
      '/home/foo/bar'
      >>> os.path.join('/home/foo', '/home/bar')
      '/home/bar'

   On Windows, the drive is not reset when a rooted path segment
   (e.g., "r'\foo'") is encountered. If a segment is on a different
   drive or is an absolute path, all previous segments are ignored and
   the drive is reset. For example:

      >>> os.path.join('c:\\', 'foo')
      'c:\\foo'
      >>> os.path.join('c:\\foo', 'd:\\bar')
      'd:\\bar'

   Note that since there is a current directory for each drive,
   "os.path.join("c:", "foo")" represents a path relative to the
   current directory on drive "C:" ("c:foo"), not "c:\foo".

   バージョン 3.6 で変更: *path* と *paths* が *path-like object* を受
   け付けるようになりました。

os.path.normcase(path, /)

   パス名の大文字・小文字を正規化します。 Windowsでは、パス名にある文
   字を全て小文字に、スラッシュをバックスラッシュに変換します。 他のオ
   ペレーティングシステムでは、パスを変更せずに返します。

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

os.path.normpath(path)

   パスを正規化します。余分な区切り文字や上位レベル参照を除去し、
   "A//B"、"A/B/"、"A/./B" や "A/foo/../B" などはすべて "A/B" になりま
   す。この文字列操作は、シンボリックリンクを含むパスの意味を変えてし
   まう場合があります。Windows では、スラッシュをバックスラッシュに変
   換します。大文字小文字の正規化には "normcase()" を使用してください
   。

   注釈:

     On POSIX systems, in accordance with IEEE Std 1003.1 2013
     Edition; 4.13 Pathname Resolution, if a pathname begins with
     exactly two slashes, the first component following the leading
     characters may be interpreted in an implementation-defined
     manner, although more than two leading characters shall be
     treated as a single character.

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

os.path.realpath(path, /, *, strict=False)

   Return the canonical path of the specified filename, eliminating
   any symbolic links encountered in the path (if they are supported
   by the operating system). On Windows, this function will also
   resolve MS-DOS (also called 8.3) style names such as "C:\\PROGRA~1"
   to "C:\\Program Files".

   By default, the path is evaluated up to the first component that
   does not exist, is a symlink loop, or whose evaluation raises
   "OSError". All such components are appended unchanged to the
   existing part of the path.

   Some errors that are handled this way include "access denied", "not
   a directory", or "bad argument to internal function". Thus, the
   resulting path may be missing or inaccessible, may still contain
   links or loops, and may traverse non-directories.

   This behavior can be modified by keyword arguments:

   If *strict* is "True", the first error encountered when evaluating
   the path is re-raised. In particular, "FileNotFoundError" is raised
   if *path* does not exist, or another "OSError" if it is otherwise
   inaccessible.

   If *strict* is "os.path.ALLOW_MISSING", errors other than
   "FileNotFoundError" are re-raised (as with "strict=True"). Thus,
   the returned path will not contain any symbolic links, but the
   named file and some of its parent directories may be missing.

   注釈:

     This function emulates the operating system's procedure for
     making a path canonical, which differs slightly between Windows
     and UNIX with respect to how links and subsequent path components
     interact.Operating system APIs make paths canonical as needed, so
     it's not normally necessary to call this function.

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

   バージョン 3.8 で変更: Windows においてシンボリックリンクとジャンク
   ションが解決されるようになりました。

   バージョン 3.10 で変更: *strict* 引数が追加されました。

   バージョン 3.14 で変更: The "ALLOW_MISSING" value for the *strict*
   parameter was added.

os.path.ALLOW_MISSING

   Special value used for the *strict* argument in "realpath()".

   Added in version 3.14.

os.path.relpath(path, start=os.curdir)

   カレントディレクトリあるいはオプションの *start* ディレクトリからの
   *path* への相対パスを返します。これはパス計算で行っており、ファイル
   システムにアクセスして *path* や *start* の存在や性質を確認すること
   はありません。Windowsでは、 *path* と *start* が異なるドライブの場
   合、 "ValueError" を送出します。

   *start* defaults to "os.curdir".

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

os.path.samefile(path1, path2, /)

   引数の両パス名が同じファイルまたはディレクトリを参照している場合、
   "True" を返します。これは、デバイス番号と i-node 番号で決定されます
   。どちらかのパス名への "os.stat()" 呼び出しが失敗した場合、例外が送
   出されます。

   バージョン 3.2 で変更: Windows サポートを追加しました。

   バージョン 3.4 で変更: Windows が他のプラットフォームと同じ実装を使
   用するようになりました。

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

os.path.sameopenfile(fp1, fp2)

   ファイル記述子 *fp1* と *fp2* が同じファイルを参照していたら "True"
   を返します。

   バージョン 3.2 で変更: Windows サポートを追加しました。

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

os.path.samestat(stat1, stat2, /)

   stat タプル *stat1* と *stat2* が同じファイルを参照していれば
   "True" を返します。これらのタプルは "os.fstat()" 、 "os.lstat()" あ
   るいは "os.stat()" の返り値で構いません。この関数は "samefile()" と
   "sameopenfile()" を使用した比較に基いて実装しています。

   バージョン 3.4 で変更: Windows サポートを追加しました。

os.path.split(path, /)

   Split the pathname *path* into a pair, "(head, tail)" where *tail*
   is the last pathname component and *head* is everything leading up
   to that.  The *tail* part will never contain a slash; if *path*
   ends in a slash, *tail* will be empty.  If there is no slash in
   *path*, *head* will be empty.  If *path* is empty, both *head* and
   *tail* are empty.  Trailing slashes are stripped from *head* unless
   it is the root (one or more slashes only).  In all cases,
   "join(head, tail)" returns a path to the same location as *path*
   (but the strings may differ).  Also see the functions "join()",
   "dirname()" and "basename()".

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

os.path.splitdrive(path, /)

   パス名 *path* を "(drive, tail)" のペアに分割します。*drive* はマウ
   ントポイントか空文字列になります。ドライブ指定をサポートしていない
   システムでは、*drive* は常に空文字になります。どの場合でも、"drive
   + tail" は *path* と等しくなります。

   Windows では、パス名はドライブ名/UNC 共有ポイントと相対パスに分割さ
   れます。

   パスがドライブレターを含む場合、ドライブレターにはコロンまでが含ま
   れます:

      >>> splitdrive("c:/dir")
      ("c:", "/dir")

   If the path contains a UNC path, drive will contain the host name
   and share:

      >>> splitdrive("//host/computer/dir")
      ("//host/computer", "/dir")

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

os.path.splitroot(path, /)

   Split the pathname *path* into a 3-item tuple "(drive, root, tail)"
   where *drive* is a device name or mount point, *root* is a string
   of separators after the drive, and *tail* is everything after the
   root. Any of these items may be the empty string. In all cases,
   "drive + root + tail" will be the same as *path*.

   On POSIX systems, *drive* is always empty. The *root* may be empty
   (if *path* is relative), a single forward slash (if *path* is
   absolute), or two forward slashes (implementation-defined per IEEE
   Std 1003.1-2017; 4.13 Pathname Resolution.) For example:

      >>> splitroot('/home/sam')
      ('', '/', 'home/sam')
      >>> splitroot('//home/sam')
      ('', '//', 'home/sam')
      >>> splitroot('///home/sam')
      ('', '/', '//home/sam')

   On Windows, *drive* may be empty, a drive-letter name, a UNC share,
   or a device name. The *root* may be empty, a forward slash, or a
   backward slash. For example:

      >>> splitroot('C:/Users/Sam')
      ('C:', '/', 'Users/Sam')
      >>> splitroot('//Server/Share/Users/Sam')
      ('//Server/Share', '/', 'Users/Sam')

   Added in version 3.12.

os.path.splitext(path, /)

   Split the pathname *path* into a pair "(root, ext)"  such that
   "root + ext == path", and the extension, *ext*, is empty or begins
   with a period and contains at most one period.

   If the path contains no extension, *ext* will be "''":

      >>> splitext('bar')
      ('bar', '')

   If the path contains an extension, then *ext* will be set to this
   extension, including the leading period. Note that previous periods
   will be ignored:

      >>> splitext('foo.bar.exe')
      ('foo.bar', '.exe')
      >>> splitext('/foo/bar.exe')
      ('/foo/bar', '.exe')

   Leading periods of the last component of the path are considered to
   be part of the root:

      >>> splitext('.cshrc')
      ('.cshrc', '')
      >>> splitext('/foo/....jpg')
      ('/foo/....jpg', '')

   バージョン 3.6 で変更: *path-like object* を受け入れるようになりま
   した。

os.path.supports_unicode_filenames

   ファイル名に任意の Unicode 文字列を (システムの制限内で) 使用できる
   場合は "True" になります。
