Python_API_File and Directory Access_os.path.split

本文详细介绍了Python标准库os模块中的path.split()函数用法,包括如何将文件路径分割为目录和文件名两部分,并提供了具体示例。适用于需要处理文件路径的Python开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

API文档:

os.path.split(path)
Split the pathname path into a pair, (head, tail) where tail is the last pathname component andhead is everything leading up to that. Thetail part will never contain a slash; ifpath ends in a slash, tail will be empty. If there is no slash inpath, head will be empty. Ifpath is empty, both head andtail are empty. Trailing slashes are stripped fromhead unless it is the root (one or more slashes only). In all cases,join(head,tail) returns a path to the same location as path (but the strings may differ).

翻译文档:

将文件和文件路径进行分离,返回(path,filename)这样一个的元组

如果path中不包含文件文件名则返回(path,''),如果path中不包含路径则返回('',fileName)


例子:

import os

pathStr = 'F:\\pyWorkSpace\\notes\\dPython\\music\\_singles\\aa.mp3'
print os.path.split(pathStr)

print type(os.path.split(pathStr))
(filePath,fileName)=os.path.split(pathStr)
print filePath
print fileName

print os.path.split('aa.mp3')
print os.path.split('C:\\')


输出:

('F:\\pyWorkSpace\\notes\\dPython\\music\\_singles', 'aa.mp3')
<type 'tuple'>
F:\pyWorkSpace\notes\dPython\music\_singles
aa.mp3
('', 'aa.mp3')
('C:\\', '')



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值