These functions allow you to deal with path names and
directory trees. To use them, import the
os module and then use
os.path. For example, to get the
base name of a path , use
pos.path.basename(.p)
basename(p)Return the base name portion of a path name
string . See
psplit(), below.
commonprefix(L)For a list containing pathname
strings, return the longest string that is a
prefix of each element in L.L
exists(p)Predicate for
testing whether pathname exists.p
expanduser(p)If is a pathname starting with a tilde
character (p~), return the
equivalent full pathname; otherwise return
.p
isabs(p)Predicate for
testing whether is an absolute
pathname (e.g., starts with a slash on Unix
systems).p
isfile(p)Predicate for
testing whether refers to a regular
file, as opposed to a directory, link, or
device.p
islink(p)Predicate for testing whether
is a soft (symbolic) link.p
ismount(p)Predicate for testing whether
is a mount point, that is, whether
pp is on a different
device than its parent directory.
join(p,q)If
is an absolute path, returns q. Otherwise, if
q
is empty or ends in a slash, returns
p,
but otherwise it returns p+q.p+'/'+q
normcase(p)Return pathname with its case
normalized. On Unix systems, this does nothing,
but on Macs it lowercases p.p
samefile(p,q)Predicate for testing whether
and p
are the same file (that is, the same inode on the
same device). This method may raise an exception
if qos.stat() fails for either
argument.
split(p)Return a 2-tuple ( where H,T) is the tail end of
the pathname (not containing a slash) and
T
is everything up to the tail. If
H
ends with a slash, returns p(p,'').
If
contains no slashes, returns p('',.
The returned p) string will have its trailing
slash removed unless H is the root
directory.H
splitext(p)Returns a 2-tuple ( where R,E) is the
“extension” part of the pathname and
E is the
“root” part. If
R contains
at least one period,
p will
contain the last period and everything after that,
and E will
be everything up to but not including the last
period. If R contains no periods, returns
p(p,'').
walk(p,V,a)Walks an entire directory structure starting at pathname
.
See below for more information.p
The os.path.walk( function does
the following for every directory at or below
p,V,a)
(including p
if p
is a directory), this method calls the “visitor function”
p
with argumentsV
V(a,d,N)
where:
| The same passed to
os.path.walk(). You can
use
to provide information to the
function, or to accumulate information throughout the
traversal of the directory structure. |
| A string containing the name of the directory being visited. |
| A list of all the names within directory
. You
can remove elements from this list in place if there
are some elements of that you don't want
walk() to visit. |