基本概念

  os.path.split()通过一对链表的头和尾来划分路径名。链表的tail是是最后的路径名元素。head则是它前面的元素。

举个例子:

path name = '/home/User/Desktop/file.txt'

在上面的这个例子中,路径名字file.txt称之为tail 路径‘/home/User/Desktop/’ 称之为head。tail部分永远不会包含斜杠符号。如果这个路径名字以斜杠结束,那么tail就是为空。

如果没有斜杠在路径中,那么head是为空的。下面是详细的参数:

    path                             head                 tail
'/home/user/Desktop/file.txt' '/home/user/Desktop/' 'file.txt'
'/home/user/Desktop/' '/home/user/Desktop/' {empty}
'file.txt' {empty} 'file.txt'

实例分析

1 实例一:

# Python program to explain os.path.split() method  

# importing os module
import os # path
path = '/home/User/Desktop/file.txt' # Split the path in
# head and tail pair
head_tail = os.path.split(path) # print head and tail
# of the specified path
print("Head of '% s:'" % path, head_tail[0])
print("Tail of '% s:'" % path, head_tail[1], "\n") # path
path = '/home/User/Desktop/' # Split the path in
# head and tail pair
head_tail = os.path.split(path) # print head and tail
# of the specified path
print("Head of '% s:'" % path, head_tail[0])
print("Tail of '% s:'" % path, head_tail[1], "\n") # path
path = 'file.txt' # Split the path in
# head and tail pair
head_tail = os.path.split(path) # print head and tail
# of the specified path
print("Head of '% s:'" % path, head_tail[0])
print("Tail of '% s:'" % path, head_tail[1])

2 结果:


Head of '/home/User/Desktop/file.txt': /home/User/Desktop
Tail of '/home/User/Desktop/file.txt': file.txt Head of '/home/User/Desktop/': /home/User/Desktop
Tail of '/home/User/Desktop/': Head of 'file.txt':
Tail of 'file.txt': file.txt

3 实例二

# Python program to explain os.path.split() method  

# importing os module
import os # path
path = '' # Split the path in
# head and tail pair
head_tail = os.path.split(path) # print head and tail
# of the specified path
print("Head of '% s':" % path, head_tail[0])
print("Tail of '% s':" % path, head_tail[1]) # os.path.split() function
# will return empty
# head and tail if
# specified path is empty

4 测试结果:

Head of '':
Tail of '':

参考文档

1 经典案例

python 中的os.path.split()函数用法的更多相关文章

  1. python中split()、os.path.split()函数用法

    一.Python split() 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则仅分隔 num 个子字符串 str.split(str="", num=string ...

  2. python中的os.path.dirname与os.path.dirname(__file__)的用法

    python中的os.path.dirname的用法 os.path.dirname(path) 语法:os.path.dirname(path) 功能:去掉文件名,返回目录 如: print(os. ...

  3. python 中的split()函数和os.path.split()函数

    Python中有split()和os.path.split()两个函数: split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表. os.path.split():将文件 ...

  4. python split(),os.path.split()和os.path.splitext()函数用法

    https://blog.csdn.net/T1243_3/article/details/80170006   # -*- coding:utf-8 -*- """ @ ...

  5. python路径拼接os.path.join()函数的用法

    os.path.join()函数:连接两个或更多的路径名组件 1.如果各组件名首字母不包含’/’,则函数会自动加上 2.如果有一个组件是一个绝对路径,则在它之前的所有组件均会被舍弃 3.如果最后一个组 ...

  6. glob.glob()、os.path.split()函数、global和nonlocal关键字

    1. glob.glob() glob模块是Python最简单的模块之一, 内容非常少, 用它可以查找符合特定规则的文件路径名, 查找文件时只会用到三个匹配符: * :匹配0个或多个字符 ? : 匹配 ...

  7. python中 OS模块中 os.path.join() 函数用法简介

    基础用法 os.path.join() 用于拼接文件的路径,可以传入多个待拼接的路径 若各个路径之间不存在 " / ", 则其会自动为各个路径之间增加连接符 " / &q ...

  8. python中使用os.path.join()

    os.path.join的详细解释请移步os.path模块在使用的过程中,我使用如下代码: import ospath = "F:/gts/gtsdate/"b = os.path ...

  9. python中的os.path.dirname(__file__)的使用

    在编程时,我们要获取当前文件所在的路径,以适合所有的工程,建立相对路径. python的os.path.dirname(__file__)非常好用,建议大家使用: import os FILE = o ...

随机推荐

  1. django 自定义模版过滤器

    自定义的模版过滤器必须要放在app中,并且该app必须在INSTALLED_APPS中进行安装.然后再在这个app下面创建一个python包叫做templatetags(这个名字是固定的,不能随意更改 ...

  2. 前端学习笔记系列一:2 Vue的单文件组件

    (1)非单文件vue组件和单文件vue组件的一般写法 一个完整的vue组件会包括三个部分:一是template模板部分,二是js程序逻辑部分,三是css样式部分.每个组件都有属于自己的模板,js和样式 ...

  3. C# webkit 内核浏览器 访问https 网站 提示 Problem with the SSL CA cert (path? access rights?)

    C# webkit 内核浏览器 访问https 网站 提示 Problem with the SSL CA cert (path? access rights?) 解决方法: 陈凯文11112014- ...

  4. UVALive 6853(dp)

    题意:已知有n个城市,某歌手每月进行一场演唱会,共持续c个月,可连续两个月在同一个城市.城市间的路费已给出,且已知每个城市在第k(1<=k<=c)个月举办演唱会的所得利润,求最终的最大利润 ...

  5. 超赞!苹果新一代iPad确定:外形大变样

    导读 除了iPhone.新MacBook Pro外,苹果还准备新款的入门版iPad,这么来看的话,他们要发布的新品真的是太多了. 据产业链最新消息称,苹果将在今年9月份更新入门版iPad,具体来说就是 ...

  6. Java笔记--异常

    1.异常分为两类: --1)Error:Java虚拟机无法解决的严重问题(例如资源耗尽等): --2)Exception:其他编程错误或偶然的外在因素导致的一般性问题(例如空指针异常.读取的文件不存在 ...

  7. Ubuntu下C++编译指令总结

    本实例只是简单的调用了一个libcurl.a的静态库,实例代码如下: #include <curl/curl.h> #include <iostream> using name ...

  8. 008、MySQL日期时间格式化输出

    #日期格式化 SELECT date_format( '2008/08/08 22:23:01', '%Y-%m-%d-%H--%i--%s' ); 不忘初心,如果您认为这篇文章有价值,认同作者的付出 ...

  9. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-minus

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  10. Golang的运算符-比较运算符

    Golang的运算符-比较运算符 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.比较运算符概述 比较运算符也称为关系运算符,比较运算符返回的类型为bool类型,常见的比较运算符 ...