python3從零學習-5.4.2、常見路徑操作os.path

      該模塊在路徑名上實現了一些有用的功能:如需讀取或寫入文件,請參見 open() ;有關訪問文件系統的信息,請參見 os 模塊。路徑參數可以字符串或字節形式傳遞。我們鼓勵應用程序將文件名錶示爲(Unicode)字符串。不幸的是,某些文件名在Unix上可能無法用字符串表示,因此在Unix上平臺上需要支持任意文件名的應用程序,應使用字節對象來表示路徑名。反之亦然,在Windows平臺上僅使用字節對象,不能表示的所有文件名(以標準 mbcs 編碼),因此Windows應用程序應使用字符串對象來訪問所有文件。

註解 由於不同的操作系統具有不同的路徑名稱約定,因此標準庫中有此模塊的幾個版本。 os.path 模塊始終是適合Python運行的操作系統的路徑模塊,因此可用於本地路徑。但是,如果操作的路徑 總是 以一種不同的格式顯示,那麼也可以分別導入和使用各個模塊。它們都具有相同的界面:

posixpath 用於Unix 樣式的路徑

ntpath 用於 Windows 路徑

macpath 用於舊 MacOS 樣式的路徑

 

os.path.abspath(path)

    返回路徑 path 的絕對路徑(標準化的)。在大多數平臺上,這等同於用 normpath(join(os.getcwd(), path)) 的方式調用 normpath() 函數。

 

os.path.basename(path)

    返回路徑 path 的基本名稱。這是將 path 傳入函數 split() 之後,返回的一對值中的第二個元素。請注意,此函數的結果與Unix basename 程序不同。basename 在 '/foo/bar/' 上返回 'bar',而 basename() 函數返回一個空字符串 ('')。

 

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.

 

os.path.commonprefix(list)

    接受包含多個路徑的 列表,返回所有路徑的最長公共前綴(逐字符比較)。如果 列表 爲空,則返回空字符串 ('')。

 

os.path.dirname(path)

    返回路徑 path 的目錄名稱。這是將 path 傳入函數 split() 之後,返回的一對值中的第一個元素。

 

os.path.exists(path)

    如果 path 指向一個已存在的路徑或已打開的文件描述符,返回 True。對於失效的符號鏈接,返回 False。在某些平臺上,如果使用 os.stat() 查詢到目標文件沒有執行權限,即使 path 確實存在,本函數也可能返回 False。

 

    在 3.3 版更改: path 現在可以是一個整數:如果該整數是一個已打開的文件描述符,返回 True,否則返回 False。

 

os.path.lexists(path)

    如果 path 指向一個已存在的路徑,返回 True。對於失效的符號鏈接,返回 False。在缺失 os.lstat() 的平臺上等同於 exists()。

 

os.path.expanduser(path)

    在 Unix 和 Windows 上,將參數中開頭部分的 ~ 或 ~user 替換爲當前 用戶 的家目錄並返回。

    在 Unix 上,開頭的 ~ 會被環境變量 HOME 代替,如果變量未設置,則通過內置模塊 pwd 在 password 目錄中查找當前用戶的主目錄。以 ~user 開頭則直接在 password 目錄中查找。

    在 Windows 上,如果設置了 HOME 和 USERPROFILE 則將使用它們,否則將使用 HOMEPATH 和 HOMEDRIVE 的組合。 原本的 ~user 處理方式爲從上述方法所生成的用戶路徑中截去最後一級目錄。

    如果展開路徑失敗,或者路徑不是以波浪號開頭,則路徑將保持不變。

 

os.path.expandvars(path)

    輸入帶有環境變量的路徑作爲參數,返回展開變量以後的路徑。$name 或 ${name} 形式的子字符串被環境變量 name 的值替換。格式錯誤的變量名稱和對不存在變量的引用保持不變。

    在 Windows 上,除了 $name 和 ${name} 外,還可以展開 %name%。

 

os.path.getatime(path)

    Return the time of last access of 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.

 

    If os.stat_float_times() returns True, the result is a floating point number.

 

os.path.getmtime(path)

    Return the time of last modification of 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.

 

    If os.stat_float_times() returns True, the result is a floating point number.

 

os.path.getctime(path)

    返回 path 在系統中的 ctime,在有些系統(比如 Unix)上,它是元數據的最後修改時間,其他系統(比如 Windows)上,它是 path 的創建時間。返回值是一個數,爲紀元秒數(參見 time 模塊)。如果該文件不存在或不可訪問,則拋出 OSError 異常。

 

os.path.getsize(path)

    返回 path 的大小,以字節爲單位。如果該文件不存在或不可訪問,則拋出 OSError 異常。

 

os.path.isabs(path)

    如果 path 是一個絕對路徑,則返回 True。在 Unix 上,它就是以斜槓開頭,而在 Windows 上,它可以是去掉驅動器號後以斜槓(或反斜槓)開頭。

 

os.path.isfile(path)

    如果 path 是 現有的 常規文件,則返回 True。本方法會跟蹤符號鏈接,因此,對於同一路徑,islink() 和 isfile() 都可能爲 True。

 

os.path.isdir(path)

    如果 path 是 現有的 目錄,則返回 True。本方法會跟蹤符號鏈接,因此,對於同一路徑,islink() 和 isdir() 都可能爲 True。

 

os.path.islink(path)

    如果 path 指向的 現有 目錄條目是一個符號鏈接,則返回 True。如果 Python 運行時不支持符號鏈接,則總是返回 False。

 

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. 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.

 

os.path.join(path, *paths)

    合理地拼接一個或多個路徑部分。返回值是 path 和 *paths 所有值的連接,每個非空部分後面都緊跟一個目錄分隔符 (os.sep),除了最後一部分。這意味着如果最後一部分爲空,則結果將以分隔符結尾。如果參數中某個部分是絕對路徑,則絕對路徑前的路徑都將被丟棄,並從絕對路徑部分開始連接。

 

    在 Windows 上,遇到絕對路徑部分(例如 r'\foo')時,不會重置盤符。如果某部分路徑包含盤符,則會丟棄所有先前的部分,並重置盤符。請注意,由於每個驅動器都有一個“當前目錄”,所以 os.path.join("c:", "foo") 表示驅動器 C: 上當前目錄的相對路徑 (c:foo),而不是 c:\foo。

 

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).

 

os.path.normpath(path)

    通過摺疊多餘的分隔符和對上級目錄的引用來標準化路徑名,所以 A//B、A/B/、A/./B 和 A/foo/../B 都會轉換成 A/B。這個字符串操作可能會改變帶有符號鏈接的路徑的含義。在 Windows 上,本方法將正斜槓轉換爲反斜槓。要規範大小寫,請使用 normcase()。

 

os.path.realpath(path)

    返回指定文件的規範路徑,消除路徑中存在的任何符號鏈接(如果操作系統支持)。

 

os.path.relpath(path, start=os.curdir)

    返回從當前目錄或 start 目錄(可選)到達 path 之間要經過的相對路徑。這僅僅是對路徑的計算,不會訪問文件系統來確認 path 或 start 的存在性或屬性。

    開始 默認爲 os.curdir

 

os.path.samefile(path1, path2)

    如果兩個路徑都指向相同的文件或目錄,則返回 True。這由設備號和 inode 號確定,在任一路徑上調用 os.stat() 失敗則拋出異常。

 

os.path.sameopenfile(fp1, fp2)

    如果文件描述符 fp1 和 fp2 指向相同文件,則返回 True。

 

os.path.samestat(stat1, stat2)

    如果 stat 元組 stat1 和 stat2 指向相同文件,則返回 True。這些 stat 元組可能是由 os.fstat()、os.lstat() 或 os.stat() 返回的。本函數實現了 samefile() 和 sameopenfile() 底層所使用的比較過程。

 

os.path.split(path)

    將路徑 path 拆分爲一對,即 (head, tail),其中,tail 是路徑的最後一部分,而 head 裏是除最後部分外的所有內容。tail 部分不會包含斜槓,如果 path 以斜槓結尾,則 tail 將爲空。如果 path 中沒有斜槓,head 將爲空。如果 path 爲空,則 head 和 tail 均爲空。head 末尾的斜槓會被去掉,除非它是根目錄(即它僅包含一個或多個斜槓)。在所有情況下,join(head, tail) 指向的位置都與 path 相同(但字符串可能不同)。另請參見函數 dirname() 和 basename()。

 

os.path.splitdrive(path)

    將路徑 path 拆分爲一對,即 (drive, tail),其中 drive 是掛載點或空字符串。在沒有驅動器概念的系統上,drive 將始終爲空字符串。在所有情況下,drive + tail 都與 path 相同。

    在 Windows 上,本方法將路徑拆分爲驅動器/UNC 根節點和相對路徑。

    如果路徑 path 包含盤符,則 drive 將包含冒號及冒號前面的所有內容。例如 splitdrive("c:/dir") 返回 ("c:", "/dir")。

    如果 path 是一個 UNC 路徑,則 drive 將包含主機名和共享點,但不包括第四個分隔符。例如 splitdrive("//host/computer/dir") 返回 ("//host/computer", "/dir")。

 

os.path.splitext(path)

    將路徑 path 拆分爲一對,即 (root, ext),使 root + ext == path,其中 ext 爲空或以英文句點開頭,且最多包含一個句點。路徑前的句點將被忽略,例如 splitext('.cshrc') 返回 ('.cshrc', '')。

 

os.path.supports_unicode_filenames

    如果(在文件系統限制下)允許將任意 Unicode 字符串用作文件名,則爲 True。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章