os.path.abspath(relative_path)
返回绝对路径
abspath = normpath(join(os.getcwd(), relative_path))
Sample:
当前路径是 /home/justtest,则:
abspath('./code/test.py') => '/home/justtest/code/test.py
os.path.relpath(path[, start])
返回相对路径, start的默认值是 os.curdir
假设当前路径是 /home/justtest
relpath('/home/justtest/test.py') => 'test.py' relpath('/home/anothertest/test.py', '/home/anothertest') => 'test.py'
os.path.basename(path)
返回文件名
basname(path) = split(path)[1]
basename('/home/justtest/test.py') =>'test.py' basename('/home/justtest/') =>''
os.path.dirname(path)
返回目录名,不包含文件名。注意:返回的路径名不含最后的斜杠
dirname(path) = split(path)[0]
dirname('/home/justtest/test.py') =>/home/justtest' dirname('/home/justtest/') =>'/home/justtest'
os.path.split(path)
将 path 分解成 (路径, 文件名)
split('/home/justtest/test.py') = ('/home/justtest', 'test.py') split('/home/justtest/') = ('/home/justtest', '')
os.path.join(path1[, path2[, ...]])
合并多个路径
join('/home', 'justtest', 'test.py') => '/home/justtest/test.py' join('/home/justtest', 'test.py') => '/home/justtest/test.py'
os.path.normpath(path)
将路径正规化:去除多余的分隔符,将 . 和 .. 变成真实路径,处理错误的斜杠
normpath('\home/justtest') => '\\home/justtest' normpath('/home/./justtest') => '/home/justtest' normpath('/home/../justtest') => '/justtest'
os.path.exists(path)
文件或路径是否存在并且有权限访问
os.path.isabs(path), isfile(path), isdir(path), islink(path)
isabs: 是否绝对路径
isfile: 是否文件
isdir: 是否路径
islink: 是否link
os.path.walk(path, callback, arg)
遍历路径,对路径中的每个文件调用callback函数
callback函数的原型如下:
callback(arg, path, files) @arg: walk函数的参数 @path: 路径 @files: 路径下的所有文件