8.2 Standard Module posixpath

 

This module implements some useful functions on POSIX pathnames.

Do not import this module directly. Instead, import the module os and use os.path.  

basename (p)
Return the base name of pathname p. This is the second half of the pair returned by posixpath.split(p).

commonprefix (list)
Return the longest string that is a prefix of all strings in list. If list is empty, return the empty string ('').

exists (p)
Return true if p refers to an existing path.

expanduser (p)
Return the argument with an initial component of "~" or "~user" replaced by that user's home directory. An initial "~" is replaced by the environment variable $HOME; an initial "~user" is looked up in the password directory through the built-in module pwd . If the expansion fails, or if the path does not begin with a tilde, the path is returned unchanged.

expandvars (p)
Return the argument with environment variables expanded. Substrings of the form "$name" or "${name}" are replaced by the value of environment variable name. Malformed variable names and references to non-existing variables are left unchanged.

isabs (p)
Return true if p is an absolute pathname (begins with a slash).

isfile (p)
Return true if p is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.

isdir (p)
Return true if p is an existing directory. This follows symbolic links, so both islink() and isdir() can be true for the same path.

islink (p)
Return true if p refers to a directory entry that is a symbolic link. Always false if symbolic links are not supported.

ismount (p)
Return true if pathname p is a mount point: a point in a file system where a different file system has been mounted. The function checks whether p's parent, "p/..", is on a different device than p, or whether "p/.." and p point to the same i-node on the same device -- this should detect mount points for all Unix and POSIX variants.

join (p[, q[, ...]])
Joins one or more path components intelligently. If any component is an absolute path, all previous components are thrown away, and joining continues. The return value is the concatenation of p, and optionally q, etc., with exactly one slash ('/') inserted between components, unless p is empty.

normcase (p)
Normalize the case of a pathname. On Unix, this returns the path unchanged; on case-insensitive filesystems, it converts the path to lowercase. On Windows, it also converts forward slashes to backward slashes.

normpath (p)
Normalize a pathname. This collapses redundant separators and up-level references, e.g. A//B, A/./B and A/foo/../B all become A/B. It does not normalize the case (use normcase() for that). On Windows, it does converts forward slashes to backward slashes.

samefile (p, q)
Return true if both pathname arguments refer to the same file or directory (as indicated by device number and i-node number). Raise an exception if a os.stat() call on either pathname fails.

split (p)
Split the pathname p in 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 p ends in a slash, tail will be empty. If there is no slash in p, head will be empty. If p 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 nearly all cases, join(head, tail) equals p (the only exception being when there were multiple slashes separating head from tail).

splitext (p)
Split the pathname p in a pair (root, ext) such that root + ext == p, and ext is empty or begins with a period and contains at most one period.

walk (p, visit, arg)
Calls the function visit with arguments (arg, dirname, names) for each directory in the directory tree rooted at p (including p itself, if it is a directory). The argument dirname specifies the visited directory, the argument names lists the files in the directory (gotten from os.listdir(dirname)). The visit function may modify names to influence the set of directories visited below dirname, e.g., to avoid visiting certain parts of the tree. (The object referred to by names must be modified in place, using del or slice assignment.)

guido@python.org