Python os.path.help

发布时间:2019-08-23 08:00:56编辑:auto阅读(1316)

    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 an alias for this
        module on Posix systems; on other systems (e.g. Mac, Windows),
        os.path provides the same operations in a manner specific to that
        platform, and is an alias to another module (e.g. macpath, ntpath).
        Some of this can actually be useful on non-Posix systems too, e.g.
        for manipulation of the pathname component of URLs.
    #使用os.path
    FUNCTIONS
        abspath(path)
            Return an absolute path.
    #返回的绝对路径。
    exp:>>> os.path.abspath('.')
    '/root/python'
        basename(p)
            Returns the final component of a pathname
    #返回一个路径名的最后一个组件,同split差不多,都是找最后以"/"+1结尾,并return p[i:]
    exp:>>> os.path.basename('./new.txt')
    'new.txt'
        commonprefix(m)
            Given a list of pathnames, returns the longest common leading component
    #鉴于路径名的列表,返回的最长公共领先的组件
        dirname(p)
            Returns the directory component of a pathname
    #返回一个路径名的目录部分,查找最后"/"+1的索引,并打印之前的数据。如果没有"/"则去掉,正常目录减掉最后那个"/",非正常目录去除最后/右边的字符串。如果无"/"返回空
    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
    #测试路径是否存在。损坏的符号链接返回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,
            do nothing.
    #返回用户的绝对路径
    exp:>>> os.path.expanduser('~/python')
    '/root/python'
        expandvars(path)
            Expand shell variables of form $var and ${var}.  Unknown variables
            are left unchanged.
    #展开变量$var和${var}
        getatime(filename)
            Return the last access time of a file, reported by os.stat().
    #返回最后一次访问文件的时间,报告由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 the last modification time of a file, reported by os.stat().
    #返回mtime
        getsize(filename)
            Return the size of a file, reported by os.stat().
    #返回一个文件的大小,报告的由os.stat()。
        isabs(s)
            Test whether a path is absolute
    #测试路径是否是绝对的,如果接收字符串是以'/'开头则返回True。
    exp:>>> os.path.isabs('./new.txt')
    False
    >>> os.path.isabs('/root/python/new.txt')
    True
        isdir(s)
            Return true if the pathname refers to an 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 is a mount point
    #测试路径是否是一个挂载点
    exp:>>> os.path.ismount('/python')
    True
        join(a, *p)
            Join two or more pathname components, inserting '/' as needed.
            If any component is an absolute path, all previous path components
            will be discarded.
    #将多个路径组合后返回,第一个绝对路径之前的参数将被忽略。
    #如果*p以"/"开头直接返回*p,如果a等于空(exp )或者路径以"/"结尾(exp "/root/www/")则a+(*p)否则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
    #测试路径是否存在。返回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
    #正常化的路径名的情况下。在POSIX下直接返回原值。
    exp:>>> os.path.normcase("c:\windows\system32")
    'c:\\windows\\system32'
        normpath(path)
            Normalize path, eliminating double slashes, etc.
    #正常化的路径,消除双斜线,等等。
    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 the canonical path of the specified filename, eliminating any
            symbolic links 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
    #返回的路径相对版本,如果空值,返回错误"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
    #测试两个路径名是否引用同一实际文件,如果2个路径指向同样的文件或目录,返回True
    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 be empty.
    #分割文件名与目录(事实上,如果你完全使用目录,它也会将最后一个目录作为文件名而分离,同时它不会判断文件或目录是否存在)
    #找出"/"在(p)中出现的最后一次,并以倒数第二次以索引进行分割。
    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.
    # 作用于windows/dos/nt,在unix永远返回空。
        splitext(p)
            Split the extension from a pathname.
            Extension is everything from the last dot to the end, ignoring
            leading dots.  Returns "(root, ext)"; ext may be empty.
    #分离文件名与扩展名,以最后"."号分割,返回一个元组。
    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 at 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 will only recurse into the subdirectories whose names remain in
            fnames; this can be 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 is always passed to func.  It can be used, e.g., to pass
            a filename pattern, or a mutable object designed to accumulate
            statistics.  Passing None for arg is common.
    #os.path.walk()
    函数声明:walk(top,func,arg)
    1>参数top表示需要遍历的目录树的路径2>参数func表示回调函数,对遍历路径进行处理.所谓回调函数,是作为某个函数的参数使用,当某个时间触发时,程序将调用定义好的回调函数处理某个任务.回调函数必须提供3个参数:第1个参数为walk()的参数tag,第2个参数表示目录列表,第3个参数表示文件列表
    3>参数arg是传递给回调参数func的元组.回调函数的一个参数必须是arg,为回调函数提供处理参数.参数arg可以为空
    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
    


关键字

上一篇: 2.Python基础

下一篇: 000-Python常量与变量