Python 获取文件地址中的目录以及文件名

您所在的位置:网站首页 python获取路径中的文件名 Python 获取文件地址中的目录以及文件名

Python 获取文件地址中的目录以及文件名

2023-09-01 01:15| 来源: 网络整理| 查看: 265

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:\temp

os.path.basename(path) Returns the final component of a pathname

import os path = r'E:\temp\CSDN' print(os.path.basename(path)) >> CSDN

os.path.splitext(path) 分离文件名与后缀

import os filename = r'CSDN.exe' print(os.path.splitext(filename)) >> ('CSDN', '.exe') ntpath

ntpath.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:\temp

ntpath.basename(path) Returns the final component of a pathname

import ntpath path = r'E:\temp\CSDN' print(ntpath.basename(path)) >> CSDN

ntpath.splitext(path) 分离文件名与后缀

import ntpath filename = r'CSDN.exe' print(ntpath.splitext(filename)) >> ('CSDN', '.exe')


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3