直接示例代码:
示例代码
import os
file_name = "../../haha/my_file.txt"
os.path.basename(file_name) # 文件名:my_file.txt
os.path.dirname(file_name) # 文件夹路径:../../haha
os.path.split(file_name) # 上面两个路径构成的字典:('../../haha', 'my_file.txt')
os.path.splitext(file_name) # 单独把文件和后缀提取出来:('../../haha/my_file', '.txt')
os.path.abspath(file_name) # 文件绝对路径:/Users/..../haha/my_file.txt