Help on module Posixpath in OS:
NAME
Posixpath-common operations on Posix pathnames.
FILE
/usr/lib/python2.6/posixpath.py
MODULE DOCS
Http://docs.python.org/library/posixpath
DESCRIPTION
Instead of importing this module directly, import OS and refer to
This module as Os.path. The "Os.path" name is a alias for this
module on POSIX systems; On the other systems (e.g. MAC, Windows),
Os.path provides the same operations in a manner specific to that
Platform, and is a alias to another module (e.g. Macpath, Ntpath).
Some of this can actually is useful on non-posix systems too, e.g.
For manipulation of the pathname component of URLs.
#使用os. Path
Functions
Abspath (PATH)
Return a absolute path.
#返回的绝对路径.
Exp:>>> os.path.abspath ('. ')
'/root/python '
BaseName (P)
Returns The final component of a pathname
#返回一个路径名的最后一个组件, similar to the split, is to find the end with "/" +1 end, and return P[i:]
Exp:>>> os.path.basename ('./new.txt ')
' New.txt '
Commonprefix (M)
Given a list of pathnames, returns longest common leading component
#鉴于路径名的列表, the longest public-leading component returned
DirName (P)
Returns the directory component of a pathname
#返回一个路径名的目录部分, find the index of the last "/" +1 and print the previous data. If no "/" is removed, the normal directory is minus the last "/", and the abnormal directory removes the last/right string. If no "/" returns null
Exp:>>> os.path.dirname ('/root/python/new.txt ')
'/root/python '
>>> os.path.dirname ("////a///a.txt")
'////a '
>>> os.path.dirname ("/a//c.ini")
'/A '
>>> a= "/////c////////a.txt"
>>> b=a.rfind ('/') +1
>>> b
14
>>> C=a[:b]
>>> C
'/////c////////'
>>> c!= '/' *len (c)
True
Exists (PATH)
Test whether a path exists. Returns False for broken Symbolic links
#测试路径是否存在. Corrupted symbolic Link returns false
Exp:>>> os.path.exists ('/root/python/new.txt ')
Rue
>>> os.path.exists ('/root/python/new.tx ')
False
Expanduser (PATH)
Expand ~ and ~user constructions. If user or $HOME is unknown,
Doing nothing.
#返回用户的绝对路径
Exp:>>> os.path.expanduser (' ~/python ')
'/root/python '
Expandvars (PATH)
Expand shell variables of form $var and ${var}. Unknown variables
are left unchanged.
#展开变量 $var and ${var}
Getatime (filename)
Return to the last access time of a file, reported by Os.stat ().
#返回最后一次访问文件的时间, reported by Os.stat ().
Exp:>>> os.path.getatime ('./new.txt ')
1369040605.3546476
>>> os.stat ('./new.txt ')
Posix.stat_result (st_mode=33060, st_ino=787083l, st_dev=64768l, St_nlink=1, St_uid=0, St_gid=0, St_size=0L, st_atime= 1369040605, st_mtime=1369040605, st_ctime=1369043721)
Getctime (filename)
Return the metadata change time of a file, reported by Os.stat ().
#返回Ctime
Getmtime (filename)
Return to modification time of a file, reported by Os.stat ().
#返回mtime
GetSize (filename)
Return the size of a file, reported by Os.stat ().
#返回一个文件的大小, reported by Os.stat ().
Isabs (s)
Test whether a path is absolute
#测试路径是否是绝对的, returns True if the receive string starts with '/'.
Exp:>>> os.path.isabs ('./new.txt ')
False
>>> os.path.isabs ('/root/python/new.txt ')
True
Isdir (s)
Return true if the pathname refers to a existing directory.
#如果是一个目录返回true
Isfile (PATH)
Test whether a path is a regular file
#如果是一个file返回true
Islink (PATH)
Test whether a path is a symbolic link
#如果是一个link返回true
Ismount (PATH)
Test whether a path to mount point
#测试路径是否是一个挂载点
Exp:>>> os.path.ismount ('/python ')
True
Join (A, *p)
Join two or more pathname components, inserting '/as needed.
If any component is a absolute path, all previous path components
would be discarded.
#将多个路径组合后返回, the parameters before the first absolute path are ignored.
#如果 *p returns *p at the beginning of "/", if a equals null (exp) or the path ends with "/" (exp "/root/www/") or an + (*P) or exp (./) '/' + (*P)
Exp:>>> os.path.join ("/root/python", "A.txt")
'/root/python/a.txt '
Lexists (PATH)
Test whether a path exists. Returns True for broken Symbolic links
#测试路径是否存在. Returns a broken symbolic link for true
Exp:>>> os.path.lexists ("/root/python")
True
>>> os.path.lexists ("/root/python1")
False
Normcase (s)
Normalize case of pathname. Has no effect under Posix
#正常化的路径名的情况下. Return the original value directly under POSIX.
Exp:>>> os.path.normcase ("C:\Windows\System32")
' C:\\Windows\\System32 '
Normpath (PATH)
Normalize path, eliminating double slashes, etc.
#正常化的路径, eliminate double slash, and so on.
Exp:>>> Os.path.normpath ("C:\\Windows")
' C:\\Windows '
>>> Os.path.normpath ("\etc\\windows")
' \\etc\\windows '
>>> Os.path.normpath ("\etc\windows")
' \\etc\\windows '
>>> Os.path.normpath ("/etc\windows")
'/etc\\windows '
>>> Os.path.normpath ("/etc/windows")
'/etc/windows
Realpath (filename)
Return to the canonical path of the specified filename, eliminating any
Symbolic links encountered in the path.
#返回指定的文件名的规范路径, eliminate any symbolic links that are encountered in the path.
Exp:>>> os.path.realpath ('/root/python/a.txt ')
'/root/python/a.txt '
RelPath (Path, start= '. ')
Return a relative version of a path
#返回的路径相对版本, if NULL, returns the error "no path specified"
Exp:
>>> os.path.relpath ('/root/python/a.txt ')
' A.txt '
Samefile (f1, F2)
Test whether two pathnames reference the same actual file
#测试两个路径名是否引用同一实际文件, returns True if 2 paths point to the same file or directory
Exp:>>> os.path.samefile ('/root/python/a.txt ', './b.txt ')
False
>>> os.path.samefile ('/root/python/a.txt ', './a.txt ')
True
Sameopenfile (FP1, FP2)
Test whether two open file objects reference the same file
#测试两个打开的文件对象是否引用同一个文件
Samestat (S1, S2)
Test whether two stat buffers reference the same file
#测试两个stat缓冲区是否引用同一个文件
Split (P)
Split a pathname. Returns tuple "(Head, tail)" where "tail" is
Everything after the final slash. Either part may is empty.
#分割文件名与目录 (In fact, if you use the directory entirely, it will also separate the last directory as a filename, and it will not determine whether the file or directory exists)
The #找出 "/" is the last occurrence in (p) and is split by the index for the second penultimate time.
Exp:>>> os.path.split ("/root/python")
('/root ', ' python ')
>>> os.path.split ("/root/python/a.txt")
('/root/python ', ' a.txt ')
>>> os.path.split ("/root/python/")
('/root/python ', ')
Splitdrive (P)
Split a pathname into drive and path. On Posix, drive is always
Empty.
# Acting on windows/dos/nt, always returning empty on Unix.
Splitext (P)
Split the extension from a pathname.
Extension is everything from the last dot to, ignoring
Leading dots. Returns "(Root, ext)"; Ext May is empty.
#分离文件名与扩展名 to the last "." Number division, returns a tuple.
Exp:>>> os.path.splitext ("/root/python/a.txt")
('/root/python/a ', ' txt ')
>>> os.path.splitext ("A.txt")
(' A ', ' txt ')
>>> os.path.splitext ("A.txt")
(' A ', ' txt ')
>>> os.path.splitext ("/a.txt")
('/A ', '. txt ')
>>> os.path.splitext ("Root/a.txt")
(' root/a ', ' txt ')
Walk (Top, func, Arg)
Directory Tree Walk with callback function.
For each directory in the directory tree rooted on top (including top
itself, but excluding '. ' and ' ... ', call func (ARG, dirname, fnames).
DirName is the name of the directory, and fnames a list of the names of
The files and subdirectories in dirname (excluding '. ' and ' ... '). Func
May modify the Fnames list in-place (e.g. via del or slice assignment),
And walk'll only recurse into the subdirectories whose names remain in
Fnames; This can is used to implement a filter, or to impose a specific
Order of visiting. No semantics are defined for, or required of, ARG,
Beyond that Arg are always passed to Func. It can be used, e.g.
A filename pattern, or a mutable object designed to accumulate
Statistics. Passing None for Arg is common.
#os. Path.walk ()
function declaration: Walk (Top,func,arg)
The 1> parameter top indicates the path 2> parameter func of the directory tree that needs to be traversed to represent the callback function. The traversal path is processed. A callback function is used as a parameter of a function, and when a time is triggered, the program invokes a defined callback function to handle a task. The callback function must provide 3 parameters: the 1th parameter is walk (), and the 2nd parameter represents the directory list, The 3rd parameter represents a list of files
3> parameter arg is the tuple passed to the callback parameter func. One of the parameters of the callback function must be ARG, which provides the processing parameters for the callback function. parameter arg can be empty
Exp:>>> def myvisit (A, dir, files):
... print dir, ":%d Files"%len (Files)
>>> os.path.walk ('/root ', Myvisit,none)
/root:12 files
/root/python:3 files
/root/python/[0-4]: 0 files
/root/install:5 files
/root/install/nagios-plugins-1.4.16:53 files
/root/install/nagios-plugins-1.4.16/pkg:3 files
DATA
__all__ = [' normcase ', ' isabs ', ' join ', ' splitdrive ', ' Split ', ' Splite ...
Altsep = None
CurDir = '. '
Defpath = ':/bin:/usr/bin '
Devnull = '/dev/null '
Extsep = '. '
Pardir = '.. '
Pathsep = ': '
Sep = '/'
Supports_unicode_filenames = False