os.path官方文檔(附翻譯)

This module implements some useful functions on pathnames. To read or write files see open(), and for accessing the filesystem see the os module. The path parameters can be passed as either strings, or bytes. Applications are encouraged to represent file names as (Unicode) character strings. Unfortunately, some file names may not be representable as strings on Unix, so applications that need to support arbitrary file names on Unix should use bytes objects to represent path names. Vice versa, using bytes objects cannot represent all file names on Windows (in the standard mbcs encoding), hence Windows applications should use string objects to access all files.node

這個模塊在路徑名上實現了一些有用的函數。要讀取或寫入文件,請參閱open(),要訪問文件系統,請參閱os模塊。路徑參數能夠做爲字符串或字節傳遞。鼓勵應用程序將文件名錶示爲(Unicode)字符串。不幸的是,有些文件名在Unix上可能沒法表示爲字符串,所以須要支持Unix上任意文件名的應用程序應該使用字節對象來表示路徑名。反之,使用字節對象不能表示Windows上的全部文件名(在標準的mbcs編碼中),所以Windows應用程序應該使用字符串對象來訪問全部文件。shell

Unlike a unix shell, Python does not do any automatic path expansions. Functions such as expanduser() and expandvars() can be invoked explicitly when an application desires shell-like path expansion. (See also the glob module.)app

與unix shell不一樣,Python不執行任何自動路徑擴展。當應用程序須要類shell路徑展開時,能夠顯式地調用expanduser()和expandvars()等函數。(參見glob模塊)。less

Note All of these functions accept either only bytes or only string objects as their parameters. The result is an object of the same type, if a path or file name is returned.函數

全部這些函數都只接受字節或字符串對象做爲參數。若是返回路徑或文件名,則結果是相同類型的對象。ui

Note Since different operating systems have different path name conventions, there are several versions of this module in the standard library. The os.path module is always the path module suitable for the operating system Python is running on, and therefore usable for local paths. However, you can also import and use the individual modules if you want to manipulate a path that is always in one of the different formats. They all have the same interface:
posixpath for UNIX-style paths
ntpath for Windows paths
macpath for old-style MacOS pathsthis

因爲不一樣的操做系統具備不一樣的路徑名稱約定,所以標準庫中有幾個版本的此模塊。操做系統。path模塊始終是適合運行Python的操做系統的路徑模塊,所以能夠用於本地路徑。不過,若是但願操做始終以不一樣格式之一顯示的路徑,還能夠導入和使用各個模塊。它們都有相同的界面:
用於unix樣式路徑的posixpath
ntpath用於Windows路徑
macpath用於老式MacOS路徑編碼

os.path.abspath(path)
Return a normalized absolutized version of the pathname path. On most platforms, this is equivalent to calling the function normpath() as follows: normpath(join(os.getcwd(), path)).
Changed in version 3.6: Accepts a path-like object.spa

返回路徑名路徑的規範化絕對版本。在大多數平臺上,這至關於按以下方式調用函數normpath(): normpath(join(os.getcwd(), path))。
在3.6版中更改:接受一個相似路徑的對象。操作系統

os.path.basename(path)
Return the base name of pathname path. This is the second element of the pair returned by passing path to the function split(). Note that the result of this function is different from the Unix basename program; where basename for '/foo/bar/' returns 'bar', the basename() function returns an empty string ('').
Changed in version 3.6: Accepts a path-like object.

返回路徑名路徑的基本名稱。這是經過將路徑傳遞給函數split()返回的對的第二個元素。注意,這個函數的結果與Unix basename程序不一樣;當'/foo/bar/'的basename返回'bar'時,basename()函數返回一個空字符串(")。
在3.6版中更改:接受一個相似路徑的對象。

os.path.commonpath(paths)
Return the longest common sub-path of each pathname in the sequence paths. Raise ValueError if paths contains both absolute and relative pathnames, or if paths is empty. Unlike commonprefix(), this returns a valid path.
Availability: Unix, Windows.
New in version 3.5.
Changed in version 3.6: Accepts a sequence of path-like objects.

返回序列路徑中每一個路徑名的最長公共子路徑。若是路徑同時包含絕對和相對路徑名,或者路徑爲空,則引起ValueError。與commonprefix()不一樣,它返回一個有效的路徑。
可用性:Unix、Windows。
新版本3.5。
在3.6版中更改:接受一系列相似路徑的對象。

os.path.commonprefix(list)
Return the longest path prefix (taken character-by-character) that is a prefix of all paths in list. If list is empty, return the empty string ('').

返回最長路徑前綴(逐個字符獲取),它是列表中全部路徑的前綴。若是list是空的,返回空字符串(")。

Note This function may return invalid paths because it works a character at a time. To obtain a valid path, see commonpath().
此函數可能返回無效路徑,由於它一次處理一個字符。要得到有效路徑,請參見commonpath()。
>>>
>>> os.path.commonprefix(['/usr/lib', '/usr/local/lib'])
'/usr/l'

>>> os.path.commonpath(['/usr/lib', '/usr/local/lib'])
'/usr'

os.path.dirname(path)
Return the directory name of pathname path. This is the first element of the pair returned by passing path to the function split().
Changed in version 3.6: Accepts a path-like object.

返回路徑名路徑的目錄名。這是經過將路徑傳遞給函數split()返回的對的第一個元素。
在3.6版中更改:接受一個相似路徑的對象。

os.path.exists(path)
Return True if path refers to an existing path or an open file descriptor. Returns False for broken symbolic links. On some platforms, this function may return False if permission is not granted to execute os.stat() on the requested file, even if the path physically exists.
Changed in version 3.3: path can now be an integer: True is returned if it is an open file descriptor, False otherwise.
Changed in version 3.6: Accepts a path-like object.

若是path引用現有路徑或打開的文件描述符,則返回True。爲斷開的符號連接返回False。在某些平臺上,若是沒有授予對請求的文件執行os.stat()的權限,則該函數可能返回False,即便該路徑實際存在。
在3.3版中更改:path如今能夠是整數:若是是打開的文件描述符,返回True,不然返回False。
在3.6版中更改:接受一個相似路徑的對象。

os.path.lexists(path)
Return True if path refers to an existing path. Returns True for broken symbolic links. Equivalent to exists() on platforms lacking os.lstat().
Changed in version 3.6: Accepts a path-like object.

若是path引用現有路徑,則返回True。對於斷開的符號連接返回True。在缺乏os.lstat()的平臺上等效exists()。
在3.6版中更改:接受一個相似路徑的對象。

os.path.expanduser(path)
On Unix and Windows, return the argument with an initial component of ~ or ~user replaced by that user’s home directory.
On Unix, an initial ~ is replaced by the environment variable HOME if it is set; otherwise the current user’s home directory is looked up in the password directory through the built-in module pwd. An initial ~user is looked up directly in the password directory.
On Windows, HOME and USERPROFILE will be used if set, otherwise a combination of HOMEPATH and HOMEDRIVE will be used. An initial ~user is handled by stripping the last directory component from the created user path derived above.
If the expansion fails or if the path does not begin with a tilde, the path is returned unchanged.
Changed in version 3.6: Accepts a path-like object.

在Unix和Windows上,返回參數,初始組件的~或~user替換爲該用戶的主目錄。
在Unix上,若是設置了初始~,則將其替換爲環境變量HOME;不然,經過內置模塊pwd在password目錄中查找當前用戶的主目錄。直接在密碼目錄中查找初始~用戶。
在Windows上,若是設置了HOME和USERPROFILE,將使用HOME和USERPROFILE,不然將使用HOMEPATH和HOMEDRIVE的組合。經過從上面派生的建立的用戶路徑中剝離最後一個目錄組件來處理初始~用戶。
若是展開失敗,或者路徑沒有以波浪號開始,則不更改返回路徑。

os.path.expandvars(path)
Return the argument with environment variables expanded. Substrings of the form $name or ${name} are replaced by the value of environment variable name. Malformed variable names and references to non-existing variables are left unchanged.
On Windows, %name% expansions are supported in addition to $name and ${name}.
Changed in version 3.6: Accepts a path-like object.

返回展開環境變量的參數。表單$name或${name}的子字符串被環境變量name的值替換。格式不正確的變量名和對不存在變量的引用保持不變。
在Windows上,除了$name和${name}以外,還支持%name%擴展。
在3.6版中更改:接受一個相似路徑的對象。

os.path.getatime(path)
Return the time of last access of path. The return value is a floating point number giving the number of seconds since the epoch (see the time module). Raise OSError if the file does not exist or is inaccessible.

返回路徑最後一次訪問的時間。返回值是一個浮點數,給出從曆元開始的秒數(參見時間模塊)。若是文件不存在或沒法訪問,則引起OSError。


os.path.getmtime(path)
Return the time of last modification of path. The return value is a floating point number giving the number of seconds since the epoch (see the time module). Raise OSError if the file does not exist or is inaccessible.
Changed in version 3.6: Accepts a path-like object.

返回路徑上次修改的時間。返回值是一個浮點數,給出從曆元開始的秒數(參見時間模塊)。若是文件不存在或沒法訪問,則引起OSError。
在3.6版中更改:接受一個相似路徑的對象。

os.path.getctime(path)
Return the system’s ctime which, on some systems (like Unix) is the time of the last metadata change, and, on others (like Windows), is the creation time for path. The return value is a number giving the number of seconds since the epoch (see the time module). Raise OSError if the file does not exist or is inaccessible.
Changed in version 3.6: Accepts a path-like object.

返回系統的ctime,在某些系統(如Unix)上,ctime是最後一次元數據更改的時間,在其餘系統(如Windows)上,ctime是path的建立時間。返回值是一個數字,給出從曆元開始的秒數(參見時間模塊)。若是文件不存在或沒法訪問,則引起OSError。
在3.6版中更改:接受一個相似路徑的對象。

os.path.getsize(path)
Return the size, in bytes, of path. Raise OSError if the file does not exist or is inaccessible.
Changed in version 3.6: Accepts a path-like object.

返回路徑的大小(以字節爲單位)。若是文件不存在或沒法訪問,則引起OSError。
在3.6版中更改:接受一個相似路徑的對象。

os.path.isabs(path)
Return True if path is an absolute pathname. On Unix, that means it begins with a slash, on Windows that it begins with a (back)slash after chopping off a potential drive letter.
Changed in version 3.6: Accepts a path-like object.

若是path是絕對路徑名,則返回True。在Unix上,這意味着它以斜槓開頭;在Windows上,它以(反)斜槓開頭,在截斷一個可能的驅動器字母以後。
在3.6版中更改:接受一個相似路徑的對象。

os.path.isfile(path)
Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.
Changed in version 3.6: Accepts a path-like object.

若是path是一個現有的常規文件,則返回True。這遵循符號連接,所以islink()和isfile()對於同一路徑均可覺得真。
在3.6版中更改:接受一個相似路徑的對象。

os.path.isdir(path)
Return True if path is an existing directory. This follows symbolic links, so both islink() and isdir() can be true for the same path.
Changed in version 3.6: Accepts a path-like object.

若是path是現有目錄,則返回True。這遵循符號連接,所以islink()和isdir()對於同一路徑均可覺得真。
在3.6版中更改:接受一個相似路徑的對象。

os.path.islink(path)
Return True if path refers to an existing directory entry that is a symbolic link. Always False if symbolic links are not supported by the Python runtime.
Changed in version 3.6: Accepts a path-like object.

若是path引用的是一個符號連接,則返回True。若是Python運行時不支持符號連接,則始終爲False。
在3.6版中更改:接受一個相似路徑的對象。

os.path.ismount(path)
Return True if pathname path is a mount point: a point in a file system where a different file system has been mounted. On POSIX, the function checks whether path’s parent, path/.., is on a different device than path, or whether path/.. and path point to the same i-node on the same device — this should detect mount points for all Unix and POSIX variants. It is not able to reliably detect bind mounts on the same filesystem. On Windows, a drive letter root and a share UNC are always mount points, and for any other path GetVolumePathName is called to see if it is different from the input path.
New in version 3.4: Support for detecting non-root mount points on Windows.
Changed in version 3.6: Accepts a path-like object.

若是pathname path是一個掛載點,則返回True:掛載了不一樣文件系統的文件系統中的一個點。在POSIX上,函數檢查path的父路徑path/..,在與path不一樣的設備上,或者path/..路徑指向同一設備上的同一i節點——這應該能夠檢測全部Unix和POSIX變體的掛載點。它不能可靠地檢測同一文件系統上的綁定掛載。在Windows上,驅動器字母根和共享UNC始終是掛載點,對於任何其餘路徑,都會調用GetVolumePathName來查看它是否與輸入路徑不一樣。
新版本3.4:支持檢測Windows上的非根掛載點。
在3.6版中更改:接受一個相似路徑的對象。

os.path.join(path, *paths)
Join one or more path components intelligently. The return value is the concatenation of path and any members of *paths with exactly one directory separator (os.sep) following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty. If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.
On Windows, the drive letter is not reset when an absolute path component (e.g., r'\foo') is encountered. If a component contains a drive letter, all previous components are thrown away and the drive letter is reset. Note that since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo.
Changed in version 3.6: Accepts a path-like object for path and paths.

智能地鏈接一個或多個路徑組件。返回值是path和*paths的全部成員的鏈接,除了最後一個部分以外,每一個非空部分後面都有一個目錄分隔符(os.sep),這意味着只有當最後一個部分是空的時候,結果纔會以分隔符結束。若是一個組件是絕對路徑,則丟棄全部之前的組件,並繼續從絕對路徑組件進行鏈接。
在Windows上,當遇到絕對路徑組件(例如r'\foo')時,驅動器字母不會重置。若是一個組件包含一個驅動器號,則丟棄全部之前的組件並重置驅動器號。注意,因爲每一個驅動器都有一個當前目錄os.path。join("c:", "foo")表示驅動器c: (c:foo)上相對於當前目錄的路徑,而不是c:\foo。
版本3.6中更改:接受path和paths的類路徑對象。

os.path.normcase(path)
Normalize the case of a pathname. On Unix and Mac OS X, this returns the path unchanged; on case-insensitive filesystems, it converts the path to lowercase. On Windows, it also converts forward slashes to backward slashes. Raise a TypeError if the type of path is not str or bytes (directly or indirectly through the os.PathLike interface).
Changed in version 3.6: Accepts a path-like object.

將路徑名的大小寫規範化。在Unix和Mac OS X上,這將返回未更改的路徑;在不區分大小寫的文件系統上,它將路徑轉換爲小寫。在Windows上,它還將前斜槓轉換爲後斜槓。若是路徑類型不是str或字節(直接或間接經過操做系統),則引起類型錯誤。PathLike接口)。
在3.6版中更改:接受一個相似路徑的對象。

os.path.normpath(path)
Normalize a pathname by collapsing redundant separators and up-level references so that A//B, A/B/, A/./B and A/foo/../B all become A/B. This string manipulation may change the meaning of a path that contains symbolic links. On Windows, it converts forward slashes to backward slashes. To normalize case, use normcase().
Changed in version 3.6: Accepts a path-like object.

經過摺疊冗餘分隔符和上層引用來規範化路徑名,以便a //B、a /B/、a /。/ B和A / foo / . ./B都變成了A/B。此字符串操做可能會更改包含符號連接的路徑的含義。在Windows上,它將前斜槓轉換爲後斜槓。若要規範化大小寫,請使用normcase()。
在3.6版中更改:接受一個相似路徑的對象。

os.path.realpath(path)
Return the canonical path of the specified filename, eliminating any symbolic links encountered in the path (if they are supported by the operating system).
Changed in version 3.6: Accepts a path-like object.

返回指定文件名的規範路徑,消除路徑中遇到的任何符號連接(若是操做系統支持這些符號連接)。
在3.6版中更改:接受一個相似路徑的對象。

os.path.relpath(path, start=os.curdir)
Return a relative filepath to path either from the current directory or from an optional start directory. This is a path computation: the filesystem is not accessed to confirm the existence or nature of path or start.
start defaults to os.curdir.
Availability: Unix, Windows.
Changed in version 3.6: Accepts a path-like object.

從當前目錄或可選的開始目錄返回到路徑的相對文件路徑。這是一個路徑計算:沒有訪問文件系統來確認路徑或啓動的存在性或本質。
start默認爲os.curdir。
可用性:Unix、Windows。
在3.6版中更改:接受一個相似路徑的對象。

os.path.samefile(path1, path2)
Return True if both pathname arguments refer to the same file or directory. This is determined by the device number and i-node number and raises an exception if an os.stat() call on either pathname fails.
Availability: Unix, Windows.
Changed in version 3.2: Added Windows support.
Changed in version 3.4: Windows now uses the same implementation as all other platforms.
Changed in version 3.6: Accepts a path-like object.

若是兩個路徑名參數都指向同一個文件或目錄,則返回True。這是由設備號和i-node號決定的,若是對任一路徑名的os.stat()調用失敗,就會引起異常。
可用性:Unix、Windows。
在3.2版中更改:增長了Windows支持。
3.4版本的改變:Windows如今使用與全部其餘平臺相同的實現。
在3.6版中更改:接受一個相似路徑的對象。

os.path.sameopenfile(fp1, fp2)
Return True if the file descriptors fp1 and fp2 refer to the same file.
Availability: Unix, Windows.
Changed in version 3.2: Added Windows support.
Changed in version 3.6: Accepts a path-like object.

若是文件描述符fp1和fp2指向同一個文件,則返回True。
可用性:Unix、Windows。
在3.2版中更改:增長了Windows支持。
在3.6版中更改:接受一個相似路徑的對象。

os.path.samestat(stat1, stat2)
Return True if the stat tuples stat1 and stat2 refer to the same file. These structures may have been returned by os.fstat(), os.lstat(), or os.stat(). This function implements the underlying comparison used by samefile() and sameopenfile().
Availability: Unix, Windows.
Changed in version 3.4: Added Windows support.
Changed in version 3.6: Accepts a path-like object.

若是stat元組stat1和stat2引用同一個文件,則返回True。這些結構可能由os.fstat()、os.lstat()或os.stat()返回。此函數實現samefile()和sameopenfile()使用的底層比較。
可用性:Unix、Windows。
在3.4版中更改:增長了Windows支持。
在3.6版中更改:接受一個相似路徑的對象。

os.path.split(path)
Split the pathname path into a pair, (head, tail) where tail is the last pathname component and head is everything leading up to that. The tail part will never contain a slash; if path ends in a slash, tail will be empty. If there is no slash in path, head will be empty. If path is empty, both head and tail are empty. Trailing slashes are stripped from head unless it is the root (one or more slashes only). In all cases, join(head, tail) returns a path to the same location as path (but the strings may differ). Also see the functions dirname() and basename().
Changed in version 3.6: Accepts a path-like object.

將路徑名路徑分割成一對(head, tail), tail是最後一個路徑名組件,head是指向該組件的全部內容。尾部永遠不會包含斜線;若是路徑以斜線結束,則tail爲空。若是路徑中沒有斜槓,則磁頭爲空。若是路徑是空的,那麼頭和尾都是空的。尾斜槓從頭部移除,除非它是根(只有一個或多個斜槓)。在全部狀況下,join(head, tail)都返回與path相同位置的路徑(可是字符串可能不一樣)。還請參見函數dirname()和basename()。
在3.6版中更改:接受一個相似路徑的對象。

os.path.splitdrive(path)
Split the pathname path into a pair (drive, tail) where drive is either a mount point or the empty 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 as path.
On Windows, splits a pathname into drive/UNC sharepoint and relative path.
If the path contains a drive letter, drive will contain everything up to and including the colon. e.g. splitdrive("c:/dir") returns ("c:", "/dir")
If the path contains a UNC path, drive will contain the host name and share, up to but not including the fourth separator. e.g. splitdrive("//host/computer/dir") returns ("//host/computer", "/dir")
Changed in version 3.6: Accepts a path-like object.

將路徑名路徑拆分爲一對(驅動器、tail),其中驅動器要麼是掛載點,要麼是空字符串。在不使用驅動器規範的系統上,驅動器老是空字符串。在全部狀況下,drive + tail將與path相同。
在Windows上,將路徑名拆分爲驅動器/UNC sharepoint和相對路徑。
若是路徑包含一個驅動器字母,驅動器將包含到冒號以前的全部內容。例如:splitdrive("c:/dir")返回("c:", "/dir")
若是路徑包含UNC路徑,驅動器將包含主機名和共享,直到但不包括第四個分隔符。例如:splitdrive("//host/computer/dir")返回("//host/computer", "/dir")
在3.6版中更改:接受一個相似路徑的對象。

os.path.splitext(path)
Split the pathname path into a pair (root, ext) such that root + ext == path, and ext is empty or begins with a period and contains at most one period. Leading periods on the basename are ignored; splitext('.cshrc') returns ('.cshrc', '').
Changed in version 3.6: Accepts a path-like object.

將路徑名路徑拆分爲一對(root, ext),這樣root + ext == path和ext爲空,或者以句點開頭,最多包含一個句點。基本名上的前導週期被忽略;splitext (。cshrc文件中)返回(「。cshrc’,」)。
在3.6版中更改:接受一個相似路徑的對象。

os.path.supports_unicode_filenames
True if arbitrary Unicode strings can be used as file names (within limitations imposed by the file system).
若是任意Unicode字符串能夠用做文件名(在文件系統施加的限制範圍內),則爲真。

相關文章
相關標籤/搜索