標籤:

python os.path模塊學習筆記

python os.path模塊學習筆記

來自專欄 we use python

os.path模塊實現了一些有用的函數用來操作目錄,如果要讀取文件 ,需要使用open()函數,如果要獲取文件系統,需要查看os模塊。

os.path模塊函數的參數僅僅接受位元組或者字元串對象作為自己的參數,這些函數的返回值是一個相同類型的對象,如果一個路徑或者文件名返回的話。

os.path模塊常用的函數:

os.path.abspath(path)------返回一個路徑的絕對路徑

os.path.basename(path)------返回一個路徑的最後的文件名,如果路徑後邊沒有具體的文件名,那麼就返回空值。和os.split比較相似

os.path.dirname(path)------返回一個路徑中的路徑,而不是最後的文件名,如果沒有文件名就全部返迴路徑名。也就是說os.path.dirname()和os.path.basename()兩個函數的返回值加起來就是一個完整的路徑,不但包含了路徑也包含了文件名。

os.path.exists(path)------True當一個路徑或者文件描述符存在的時候,有些平台上,如果文件沒有授權執行os.stat()也會返回False

Changed in version 3.3: path can now be an integer: True is returned if it is an open file descriptor, False otherwise.

os.path.getatime(path)-------返回最後一次訪問路徑的時間,但返回值是一個浮點數字

Return the time of last access of path. The return value is a number givingthe number of seconds since the epoch (see the time module). RaiseOSError if the file does not exist or is inaccessible.

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

os.path.getmtime(path)------返回最後一次路徑修改的時間,同樣也是一個浮點數

Return the time of last modification of path. The return value is a numbergiving 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 pointnumber.

os.path.getctime(path)------注意不同平台返回值所代表的意義不相同,UNIX系統上是最後metadata修改的時間,Windows上是路徑創建的時間。

Return the system』s ctime which, on some systems (like Unix) is the time of thelast 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 (seethe time module). Raise OSError if the file does not exist oris inaccessible.

os.path.getsize(path)------返回值是路徑的大小,以位元組的形式

Return the size, in bytes, of path. Raise OSError if the file doesnot exist or is inaccessible.

os.path.isabs(path)------路徑是不是絕對路徑

Return True if path is an absolute pathname. On Unix, that means itbegins with a slash, on Windows that it begins with a (back)slash after choppingoff a potential drive letter.

os.path.isfile(path)

Return True if path is an existing regular file. This follows symboliclinks, so both islink() and isfile() can be true for the same path.

os.path.isdir(path)

Return True if path is an existing directory. This follows symboliclinks, so both islink() and isdir() can be true for the same path.

os.path.islink(path)

Return True if path refers to a directory entry that is a symbolic link.Always False if symbolic links are not supported by the Python runtime.

os.path.ismount(path)------涉及到掛載知識點,不是很熟悉

Return True if pathname path is a mount point: a point in afile system where a different file system has been mounted. On POSIX, thefunction checks whether path『s parent, path/.., is on a differentdevice than path, or whether path/.. and path point to the samei-node on the same device — this should detect mount points for all Unixand POSIX variants. On Windows, a drive letter root and a share UNC arealways mount points, and for any other path GetVolumePathName is calledto see if it is different from the input path.

New in version 3.4: Support for detecting non-root mount points on Windows.

os.path.join(path, *paths)------這個函數將路徑拼接後返回,而join()函數另有他用

Join one or more path components intelligently. The return value is theconcatenation of path and any members of *paths with exactly onedirectory separator (os.sep) following each non-empty part except thelast, meaning that the result will only end in a separator if the lastpart is empty. If a component is an absolute path, all previouscomponents are thrown away and joining continues from the absolute pathcomponent.

os.path.samestat(stat1, stat2)------如果兩個文件的屬性列表相同就返回True

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 theunderlying comparison used by samefile() and sameopenfile().

os.path.split(path)------分割路徑為路徑加文件名,可以理解為讀取文件名

Split the pathname path into a pair, (head, tail) where tail is thelast pathname component and head is everything leading up to that. Thetail part will never contain a slash; if path ends in a slash, tailwill be empty. If there is no slash in path, head will be empty. Ifpath is empty, both head and tail are empty. Trailing slashes arestripped from head unless it is the root (one or more slashes only). Inall cases, join(head, tail) returns a path to the same location as path(but the strings may differ). Also see the functions dirname() andbasename().

os.path.splitdrive(path)------分割為驅動器和尾巴

Split the pathname path into a pair (drive, tail) where drive is eithera mount point or the empty string. On systems which do not use drivespecifications, drive will always be the empty string. In all cases, drive + tail will be the same as path.


推薦閱讀:

用協程和aiohttp爬虎撲步行街前100頁所有帖子
如何用100行Python代碼做出魔性聲控遊戲「八分音符醬」
Python 2 和 Python 3 有哪些主要區別?
《Python數據挖掘》筆記(八)文本中的主題建模
用 150 行 Python 代碼寫的量子計算模擬器

TAG:Python |