(原创)Python文件与文件系统系列(3)——os.path模块

  os.path 模块实现了一些操作路径名字符串的函数,可以通过

import os.path

  使用该模块,不过即使仅仅

import os

  也可以使用该模块的方法。

 

1. abspath(path)

os.path.abspath(path)

  返回参数 path 标准化后对应的绝对路径。

 例:

>>> os.path.abspath('revirew.txt')
'D:\\programs\\leetcode\\revirew.txt'

 

 2. basename(path)

 os.path.basename(path)

  basename()求参数 path 最右端的 base 部分,等于 os.path.split(path)[1]。

例:

>>> os.path.split('D:\\programs\\leetcode\\revirew.txt')
('D:\\programs\\leetcode', 'revirew.txt')
>>> os.path.basename('D:\\programs\\leetcode\\review.txt')
'review.txt'
>>> os.path.split('D:\\programs\\leetcode\\')
('D:\\programs\\leetcode', '')
>>> os.path.basename('D:\\programs\\leetcode\\')
''

   要注意,如果 path 以目录分割符结尾,os.path.split()[1] 和os.path.basename() 都将返回一个空的字符串,这个Linux中的 $ basename 命令是很不同的。

 

3. commonprefix(list)

os.path.commonprefix(list)

  参数 list 是一个由字符串组成的列表,commonprefix()返回这些字符串的最大公共前缀,注意这个函数不仅可以处理路径,还可以处理任意的字符串。

例:

>>> os.path.commonprefix(['flower', 'flow'])
'flow'

 

4. dirname(path)

os.path.dirname(path)

  返回 path 所在的目录名。等于 os.path.split(path)[0]。注意该函数只能返回参数 path 中 提供的信息,并不会自动帮你获取一个路径的父目录信息。

例:

>>> os.path.dirname('D:\\programs\\leetcode\\')
'D:\\programs\\leetcode'
>>> os.path.dirname('D:\\programs\\leetcode\\review.txt')
'D:\\programs\\leetcode'
>>> os.path.dirname('review.txt')
''

   注意返回的 结果最右端不包含路径分隔符。

 

5. exists(path)

os.path.exists(path)

  若参数对应的路径path存在,返回真值,对于无效的符号链接返回假值。有的平台上如果不能在目标路径上执行 os.stat() 函数,即使参数路径path物理存在,该函数也可能返回假值。

  等于os.access(path, F_OK)。

 

6. lexists(path)

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

 

7. expanduser(path)

 os.path.expanduser(path)

 

8. expandvars(path)

os.path.expandvars(path)

  使用环境变量的具体值替代参数 path 中的环境变量,如果 path 中提供的环境变量不存在,将不会扩展替换 path

例:

1 >>> os.path.expandvars("$HOME/FOO")
2 'C:\\Users\\ymzhang/FOO'
3 >>> os.path.expandvars("$HOMEPATH/FOO")
4 '\\Users\\ymzhang/FOO'
5 >>> os.path.expandvars("$HOMEDRIVE/FOO")
6 'C:/FOO'
7 >>> os.path.expandvars("$DARREN/FOO")
8 '$DARREN/FOO'

 

  其中第7行提供了一个不存在的环境变量名,此时Python不会对参数进行处理。Windows中,除了$varName和${varName}表示环境变量,还可以使用 %varName% 的形式表示环境变量:

1 >>> os.path.expandvars("${HOME}/foo")
2 'C:\\Users\\ymzhang/foo'
3 >>> os.path.expandvars("%HOME%/foo")
4 'C:\\Users\\ymzhang/foo'

 

 

 9. getatime(path)

os.path.getatime(path)

  返回指定路径 path 最后一次被访问的时间,返回的结果是自 epoch 以来经过的秒数,如果指定的路径不存在或者不可访问,将会抛出异常 os.error。

  注:getatime(path) 的结果就等于 os.stat(path).st_atime:

1 >>> os.path.getatime('review.txt')
2 1437488202.646196
3 >>> os.stat('review.txt').st_atime
4 1437488202.646196

 

  如果 os.stat_float_times() 返回 True,那么getatime()返回的结果是一个浮点值。

 

10. getmtime(path)

os.path.getmtime(path)

  返回指定路径 path 最后一次被修改的时间,返回的结果是自 epoch 以来经过的秒数,如果指定的路径不存在或者不可访问,将会抛出异常 os.error。

  注:getmtime(path) 的结果就等于 os.stat(path).st_mtime:

1 >>> os.path.getmtime('review.txt')
2 1437497408.4901712
3 >>> os.stat('review.txt').st_mtime
4 1437497408.4901712

 

  如果 os.stat_float_times() 返回 True,那么getmtime()返回的结果是一个浮点值。

 

11. getctime(path)

os.path.getctime(path)
  • 在Unix系统上,返回的 ctime 是指定文件 path 的元数据上一次被修改的时间;
  • 在Windows系统上,返回的 ctime 是指定文件 path 创建的时间。

  返回的结果是自 epoch 以来经过的秒数,如果指定的路径不存在或者不可访问,将会抛出异常 os.error。

  注:getmtime(path) 的结果就等于 os.stat(path).st_mtime:

1 >>> os.path.getctime('review.txt')
2 1437488202.646196
3 >>> os.stat('review.txt').st_ctime
4 1437488202.646196

 

 如果 os.stat_float_times() 返回 True,那么getctime()返回的结果是一个浮点值。

12. getsize(path)
os.path.getsize(path)

  返回指定文件 path 的大小(单位:字节),如果指定的路径不存在或者不可访问,将会抛出异常 os.error。

  注:getsize(path) 的结果就等于 os.stat(path).st_size:

例:

1 >>> os.path.getsize('review.txt')
2 570L
3 >>> os.stat('review.txt').st_size
4 570L

 

 

13. isabs(path)

 os.path.isabs(path)

  判断 path 是不是绝对路径:

  • Unix平台上,path是不是以“/”开头;
  • Windows平台上,path以 盘符 加上  os.sep  开头。

 

14. isfile(path)

os.path.isfile(path)

  判断 path 是不是一个存在的普通文件,Unix平台上,isfile()同样跟踪符号连接,此时 islink() 和 isfile() 都返回 True。 

 

15. isdir(path)

os.path.isdir(path)

  判断 path 是不是一个目录。Unix平台上,isdir()同样跟踪符号连接,此时 islink() 和 isdir() 都返回 True。

 

16. islink(path)

os.path.islink(path)

  判断 path 是不是一个符号连接,在不支持符号链接的平台上,islink()总是返回False。

  

17. ismount(path)

os.path.ismount(path)

  判断给定的path是不是一个挂载点,在不支持挂载点的平台上总是返回False.

  挂载点是一个文件系统上挂载其他文件系统的地方,

  • 如果path和path的父目录(path/..)不在一个设备上
  • or whether path/.. and path point to the same i-node on the same device 

来判断一个路径是否是挂载点。

  

18. join(path, *paths)

 os.path.join(path, *paths)

  以适合当前平台的目录分隔符连接一个或多个路径。

  返回一个连接了path*paths所有成员的路径(字符串),其间以一个目录分隔符(os.sep)分隔。如果*paths的最后一个成员是一个空字符串(""),那么连接后的结果将以目录分隔符结尾,否则连接后的结果不包括目录分隔符。

例:

>>> os.path.join("a\\b", "c\\d")
'a\\b\\c\\d'
>>> os.path.join("a\\b", "c\\d", "")
'a\\b\\c\\d\\'

  如果*paths中的某一个是绝对路径,那么这个路径前面的参数全部抛弃,连接从这个绝对路径后面开始并继续下去,最终的结果是一个绝对路径。例:

>>> os.path.join("a\\b", "c\\d", "")
'a\\b\\c\\d\\'
>>> os.path.join("a\\b", "\\c\\d")
'\\c\\d'

   可以发现,绝对路径前面的参数都被无视。  

  Windows下,*paths的某个成员是绝对路径不会改变盘符,除非某一个成员中包含盘符,那么这个路径前面的参数全部抛弃,路径连接会从新的盘符下开始。 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), notc:\foo

 

19. 

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 '/' to '\'.

 

20. normpath(path)

os.path.normpath(path)

  标准化 path 中的路径名。去除多余的目录分隔符和中间目录(如:“../”, “./”等)。

例:normpath(path)
>>> os.path.normpath("c:\\.\\test")
'c:\\test'
>>> os.path.normpath("c:\\..\\test")
'c:\\test'
  在Windows平台上,normpath(path)会把“/”转换成“\”。
21. 
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).

 

22. 

 os.path.relpath(path[, start]) 
Return a relative filepath to path either from the current directory or from an optional startdirectory. 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.
23. 
os.path.samefile(path1, path2) 
Return True if both pathname arguments refer to the same file or directory (as indicated by device number and i-node number). Raise an exception if a os.stat() call on either pathname fails.
Availability: Unix.
24. 
os.path.sameopenfile(fp1, fp2) 
Return True if the file descriptors fp1 and fp2 refer to the same file.
Availability: Unix.
25. 
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.
26. 
os.path.split(path)

  将参数 path 分割成一个二元组——(dir, base),然后进行os.path.join(dir, base)能够重新得到path。

  注意 dir 的结尾一定不含 os.sep,而整个 base 中也不含os.sep,所以如果path是以“/”或"\\"结尾,那么此时的base就是""。

  注意os.path.split()与os.path.dirname()和os.path.basename()的关系。

  t = os.path.split(path),则 t[0] 等于os.path.dirname(path), 而 t[1] 等于 os.path.basename(path)

 

27.

os.path.splitdrive(path)

  将参数 path 分割成二元组——(drive,tail),其中drive是盘符,tail是剩余的路径。

  在不支持盘符的系统(如Unix)上,drive总是""。

例:

>>> os.path.splitdrive("c:\\..\\test")
('c:', '\\..\\test')
>>> os.path.splitdrive("\\..\\test")
('', '\\..\\test')

 

28. 

os.path.splitext(path) 

  分割出文件扩展类型,将参数 path 分割成二元组——(root,ext),满足root + ext = path,其中,

  • ext 或者为"",或者最多只能包含一个句点".",如果 path 是以句点“.”开头,那么ext为空:

例:

>>> os.path.splitext('.cshrc')
('.cshrc', '')
>>> os.path.splitext("\\..\\test.txt")
('\\..\\test', '.txt')

 

29. 

os.path.splitunc(path) 

  Split the pathname path into a pair (unc, rest) so that unc is the UNC mount point (such asr'\\host\mount'), if present, and rest the rest of the path (such as r'\path\file.ext'). For paths containing drive letters, unc will always be the empty string.
  Availability: Windows.
30.
os.path.supports_unicode_filenames 
  True if arbitrary Unicode strings can be used as file names (within limitations imposed by the file system).
  New in version 2.3.
posted @ 2014-12-16 10:38  王智愚  阅读(1036)  评论(0编辑  收藏  举报