Python 获取文件地址中的目录以及文件名 |
您所在的位置:网站首页 › python获取路径中的文件名 › Python 获取文件地址中的目录以及文件名 |
os.path. 获取文件目录及文件名
os.path.split(path) Split a pathname. Return tuple (head, tail) where tail is everything after the final slash. Either part may be empty. import os path = r'E:\temp\CSDN' print(os.path.split(path)) >> ('E:\\temp', 'CSDN')os.path.dirname(path) Returns the directory component of a pathname import os path = r'E:\temp\CSDN' print(os.path.dirname(path)) >> E:\tempos.path.basename(path) Returns the final component of a pathname import os path = r'E:\temp\CSDN' print(os.path.basename(path)) >> CSDNos.path.splitext(path) 分离文件名与后缀 import os filename = r'CSDN.exe' print(os.path.splitext(filename)) >> ('CSDN', '.exe') ntpathntpath.split(path) Split a pathname. Return tuple (head, tail) where tail is everything after the final slash. Either part may be empty. import ntpath path = r'E:\temp\CSDN' print(ntpath.split(path)) >> ('E:\\temp', 'CSDN')ntpath.dirname(path) Returns the directory component of a pathname import ntpath path = r'E:\temp\CSDN' print(ntpath.dirname(path)) >> E:\tempntpath.basename(path) Returns the final component of a pathname import ntpath path = r'E:\temp\CSDN' print(ntpath.basename(path)) >> CSDNntpath.splitext(path) 分离文件名与后缀 import ntpath filename = r'CSDN.exe' print(ntpath.splitext(filename)) >> ('CSDN', '.exe') |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |