【python】os.path模块
os.path模块
os.path 模块主要用于获取文件的属性。以下是 os.path 模块的几种常用方法:
方法 | 说明 |
---|---|
os.path.abspath(path) | 返回绝对路径 |
os.path.basename(path) | 返回文件名 |
os.path.commonprefix(list) | 返回list(多个路径)中,所有path共有的最长的路径 |
os.path.dirname(path) | 返回文件路径 |
os.path.exists(path) | 如果路径 path 存在,返回 True;如果路径 path 不存在或损坏,返回 False。 |
os.path.lexists(path) | 路径存在则返回 True,路径损坏也返回 True |
os.path.expanduser(path) | 把 path 中包含的 ~ 和 ~user 转换成用户目录 |
os.path.expandvars(path) | 根据环境变量的值替换 path 中包含的 $name 和 ${name} |
os.path.getatime(path) | 返回最近访问时间(浮点型秒数) |
os.path.getmtime(path) | 返回最近文件修改时间 |
os.path.getctime(path) | 返回文件 path 创建时间 |
os.path.getsize(path) | 返回文件大小,如果文件不存在就返回错误 |
os.path.isabs(path) | 判断是否为绝对路径 |
os.path.isfile(path) | 判断路径是否为文件 |
os.path.isdir(path) | 判断路径是否为目录 |
os.path.islink(path) | 判断路径是否为链接 |
os.path.ismount(path) | 判断路径是否为挂载点 |
os.path.join(path1[, path2[, ...]]) | 把目录和文件名合成一个路径 |
os.path.normcase(path) | 转换path的大小写和斜杠 |
os.path.normpath(path) | 规范path字符串形式 |
os.path.realpath(path) | 返回path的真实路径 |
os.path.relpath(path[, start]) | 从start开始计算相对路径 |
os.path.samefile(path1, path2) | 判断目录或文件是否相同 |
os.path.sameopenfile(fp1, fp2) | 判断fp1和fp2是否指向同一文件 |
os.path.samestat(stat1, stat2) | 判断stat tuple stat1和stat2是否指向同一个文件 |
os.path.split(path) | 把路径分割成 dirname 和 basename,返回一个元组 |
os.path.splitdrive(path) | 一般用在 windows 下,返回驱动器名和路径组成的元组 |
os.path.splitext(path) | 分割路径,返回路径名和文件扩展名的元组 |
os.path.splitunc(path) | 把路径分割为加载点与文件 |
os.path.walk(path, visit, arg) | 遍历path,进入每个目录都调用visit函数,visit函数必须有3个参数(arg, dirname, names),dirname表示当前目录的目录名,names代表当前目录下的所有文件名,args则为walk的第三个参数 |
os.path.supports_unicode_filenames | 设置是否支持unicode路径名 |
实例1:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
print( os.path.basename('/root/runoob.txt') ) # 返回文件名
print( os.path.dirname('/root/runoob.txt') ) # 返回目录路径
print( os.path.split('/root/runoob.txt') ) # 分割文件名与路径
print( os.path.join('root','test','runoob.txt') ) # 将目录和文件名合成一个路径
输出:
runoob.txt
/root
('/root', 'runoob.txt')
root/test/runoob.txt
实例2:
# Python program to explain os.path.join() method
# importing os module
import os
# Path
path = "/home"
# Python program to explain os.path.join() method
# importing os module
import os
# Path
path = "/home"
# Join various path components
print(os.path.join(path, "User/Desktop", "file.txt"))
# Path
path = "User/Documents"
# Join various path components
print(os.path.join(path, "/home", "file.txt"))
# In above example '/home'
# represents an absolute path
# so all previous components i.e User / Documents
# are thrown away and joining continues
# from the absolute path component i.e / home.
# Path
path = "/User"
# Join various path components
print(os.path.join(path, "Downloads", "file.txt", "/home"))
# In above example '/User' and '/home'
# both represents an absolute path
# but '/home' is the last value
# so all previous components before '/home'
# will be discarded and joining will
# continue from '/home'
# Path
path = "/home"
# Join various path components
print(os.path.join(path, "User/Public/", "Documents", ""))
# In above example the last
# path component is empty
# so a directory separator ('/')
# will be put at the end
# along with the concatenated value
输出:
/home/User/Desktop/file.txt
/home/file.txt
/home
/home/User/Public/Documents/