Next / Previous / Contents / TCC Help System / NM Tech homepage

28.9. os.path: File and directory interface

These functions allow you to deal with path names and directory trees. To use a given method in this module, import the os module and then use os.path.method().

For example, to get the base name of a path p, use os.path.basename(p).


Return the absolute path name that is equivalent to path p.


Return the base name portion of a path name string p. See split(), below.


For a list L containing pathname strings, return the longest string that is a prefix of each element in L.


Predicate for testing whether pathname p exists.


If p is a pathname starting with a tilde character (~), return the equivalent full pathname; otherwise return p.


Predicate for testing whether p is an absolute pathname (e.g., starts with a slash on Unix systems).


Predicate for testing whether p refers to a regular file, as opposed to a directory, link, or device.


Predicate for testing whether p is a soft (symbolic) link.


Predicate for testing whether p is a mount point, that is, whether p is on a different device than its parent directory.


If q is an absolute path, returns q. Otherwise, if p is empty or ends in a slash, returns p+q, but otherwise it returns p+'/'+q.


Return pathname p with its case normalized. On Unix systems, this does nothing, but on Macs it lowercases p.


Predicate for testing whether p and q are the same file (that is, the same inode on the same device). This method may raise an exception if os.stat() fails for either argument.


Return a 2-tuple (H,T) where T is the tail end of the pathname (not containing a slash) and H is everything up to the tail. If p ends with a slash, returns (p,''). If p contains no slashes, returns ('',p). The returned H string will have its trailing slash removed unless H is the root directory.


Returns a 2-tuple (R,E) where E is the “extension” part of the pathname and R is the “root” part. If p contains at least one period, E will contain the last period and everything after that, and R will be everything up to but not including the last period. If p contains no periods, returns (p,'').


Walks an entire directory structure starting at pathname p. See below for more information.

The os.path.walk(p,V,a) function does the following for every directory at or below p (including p if p is a directory), this method calls the “visitor function” V with arguments


a The same a passed to os.path.walk(). You can use a to provide information to the V() function, or to accumulate information throughout the traversal of the directory structure.
d A string containing the name of the directory being visited.
N A list of all the names within directory d. You can remove elements from this list in place if there are some elements of d that you don't want walk() to visit.