Movatterモバイル変換
[0]ホーム
This module implements some useful functions on pathnames.
Warning:On Windows, many of these functions do not properlysupport UNC pathnames.splitunc() andismount()do handle them correctly.
- Return a normalized absolutized version of the pathnamepath.On most platforms, this is equivalent to
normpath(join(os.getcwd(),path)).New in version 1.5.2.
- Return the base name of pathnamepath. This is the second halfof the pair returned by
split(path). Note that theresult of this function is different from theUnixbasename program; wherebasename for'/foo/bar/' returns'bar', thebasename()function returns an empty string ('').
- Return the longest path prefix (taken character-by-character) that is aprefix of all paths inlist. Iflist is empty, return the empty string(
''). Note that this may return invalid paths because it works acharacter at a time.
- Return the directory name of pathnamepath. This is the firsthalf of the pair returned by
split(path).
- Return
True ifpath refers to an existing path. ReturnsFalse for broken symbolic links. On some platforms, thisfunction may returnFalse if permission is not granted toexecuteos.stat() on the requested file, even if thepath physically exists.
- Return
True ifpath refers to an existing path.ReturnsTrue for broken symbolic links. Equivalent toexists() on platforms lackingos.lstat().New in version 2.4.
- OnUnix, return the argument with an initial component of "~" or"~user" replaced by thatuser's home directory.An initial "~" is replaced by the environment variableHOME if it is set; otherwise the current user's home directoryis looked up in the password directory through the built-in modulepwd.An initial "~user" is looked up directly in thepassword directory.
On Windows, only "~" is supported; it is replaced by theenvironment variableHOME or by a combination ofHOMEDRIVE andHOMEPATH.
If the expansion fails or if thepath does not begin with a tilde, the path is returned unchanged.
- Return the argument with environment variables expanded. Substringsof the form "$name" or "${name}" arereplaced by the value of environment variablename. Malformedvariable names and references to non-existing variables are leftunchanged.
- Return the time of last access ofpath. The returnvalue is a number giving the number of seconds since the epoch (see thetime module). Raiseos.error if the file doesnot exist or is inaccessible.New in version 1.5.2.Changed in version 2.3:Ifos.stat_float_times() returns True, the result is a floating point number.
- Return the time of last modification ofpath. The returnvalue is a number giving the number of seconds since the epoch (see thetime module). Raiseos.error if the file doesnot exist or is inaccessible.New in version 1.5.2.Changed in version 2.3:Ifos.stat_float_times() returns True, the result is a floating point number.
- Return the system's ctime which, on some systems (likeUnix) is thetime of the last change, and, on others (like Windows), is thecreation time forpath. The returnvalue is a number giving the number of seconds since the epoch (see thetime module). Raiseos.error if the file doesnot exist or is inaccessible.New in version 2.3.
- Return the size, in bytes, ofpath. Raiseos.error if the file does not exist or is inaccessible.New in version 1.5.2.
- Return
True ifpath is an absolute pathname (begins with aslash).
- Return
True ifpath is an existing regular file. This followssymbolic links, so bothislink() andisfile()can be true for the same path.
- Return
True ifpath is an existing directory. This followssymbolic links, so bothislink() andisdir() canbe true for the same path.
- Return
True ifpath refers to a directory entry that is asymbolic link. AlwaysFalse if symbolic links are not supported.
- Return
True if pathnamepath is amount point: a point ina file system where a different file system has been mounted. Thefunction checks whetherpath's parent,path/.., ison a different device thanpath, or whetherpath/..andpath point to the same i-node on the same device -- thisshould detect mount points for allUnix and POSIX variants.
| join( | path1[, path2[, ...]]) |
- Join one or more path components intelligently. If any component isan absolute path, all previous components (on Windows, including theprevious drive letter, if there was one) are thrown away, and joiningcontinues. The return value is the concatenation ofpath1, andoptionallypath2, etc., with exactly one directory separator(
os.sep) inserted between components, unlesspath2 isempty. Note that on Windows, since there is a current directory foreach drive,os.path.join("c:", "foo") represents a pathrelative to the current directory on driveC: (c:foo), notc:\\foo.
- Normalize the case of a pathname. OnUnix, this returns the pathunchanged; on case-insensitive filesystems, it converts the path tolowercase. On Windows, it also converts forward slashes to backwardslashes.
- Normalize a pathname. This collapses redundant separators andup-level references so that
A//B,A/./B andA/foo/../B all becomeA/B. It does not normalize thecase (usenormcase() for that). On Windows, it convertsforward slashes to backward slashes. It should be understood that this maychange the meaning of the path if it contains symbolic links!
- Return the canonical path of the specified filename, eliminating anysymbolic links encountered in the path (if they are supported by theoperating system).New in version 2.2.
- Return
True if both pathname arguments refer to the same file ordirectory (as indicated by device number and i-node number).Raise an exception if aos.stat() call on either pathnamefails.Availability: Macintosh,Unix.
- Return
True if the file descriptorsfp1 andfp2 referto the same file.Availability: Macintosh,Unix.
- Return
True if the stat tuplesstat1 andstat2 refer tothe same file. These structures may have been returned byfstat(),lstat(), orstat(). Thisfunction implements the underlying comparison used bysamefile() andsameopenfile().Availability: Macintosh,Unix.
- Split the pathnamepath into a pair,
(head,tail) wheretail is the last pathname component andhead is everything leading up to that. Thetail part willnever contain a slash; ifpath ends in a slash,tail willbe empty. If there is no slash inpath,head will beempty. Ifpath is empty, bothhead andtail areempty. Trailing slashes are stripped fromhead unless it is theroot (one or more slashes only). In nearly all cases,join(head,tail) equalspath (the onlyexception being when there were multiple slashes separatingheadfromtail).
- Split the pathnamepath into a pair
(drive,tail) wheredrive is either a drive specification or theempty string. On systems which do not use drive specifications,drive will always be the empty string. In all cases,drive +tail will be the same aspath.New in version 1.3.
- Split the pathnamepath into a pair
(root,ext) such thatroot +ext ==path,andext is empty or begins with a period and containsat most one period.
- Split the pathnamepath into a pair
(unc,rest)so thatunc is the UNC mount point (such asr'\\host\mount'),if present, andrest the rest of the path (such asr'\path\file.ext'). For paths containing drive letters,uncwill always be the empty string.Availability: Windows.
- Calls the functionvisit with arguments
(arg,dirname,names) for each directory in thedirectory tree rooted atpath (includingpath itself, if itis a directory). The argumentdirname specifies the visiteddirectory, the argumentnames lists the files in the directory(gotten fromos.listdir(dirname)).Thevisit function may modifynames toinfluence the set of directories visited belowdirname, e.g. toavoid visiting certain parts of the tree. (The object referred to bynames must be modified in place, usingdel or sliceassignment.)Note:Symbolic links to directories are not treated as subdirectories, andthatwalk() therefore will not visit them. To visit linkeddirectories you must identify them withos.path.islink(file) andos.path.isdir(file), and invokewalk() asnecessary.
Note:The neweros.walk() generator supplies similar functionality and can be easier to use.
- supports_unicode_filenames
- True if arbitrary Unicode strings can be used as file names (withinlimitations imposed by the file system), and ifos.listdir() returns Unicode strings for a Unicodeargument.New in version 2.3.
Release 2.5.2, documentation updated on 21st February, 2008. SeeAbout this document... for information on suggesting changes.
[8]ページ先頭