Python os.path.help

  • 2020 年 1 月 10 日
  • 筆記

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:windowssystem32")  '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("etcwindows")  '\etc\windows'  >>> os.path.normpath("/etcwindows")  '/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