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 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)------注意不同平台返回值所代表的意义不相同,UNIX系统上是最后metadata修改的时间,Windows上是路径创建的时间。
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.
os.path.getsize(path)------返回值是路径的大小,以字节的形式
Return the size, in bytes, of path. Raise OSError if the file does
not exist or is inaccessible.
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.
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.
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.
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 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.
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 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.
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 the
underlying comparison used by samefile() and sameopenfile().
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().
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.