python 中的os.path.split()函数用法
基本概念
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()函数用法的更多相关文章
- python中split()、os.path.split()函数用法
一.Python split() 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则仅分隔 num 个子字符串 str.split(str="", num=string ...
- python中的os.path.dirname与os.path.dirname(__file__)的用法
python中的os.path.dirname的用法 os.path.dirname(path) 语法:os.path.dirname(path) 功能:去掉文件名,返回目录 如: print(os. ...
- python 中的split()函数和os.path.split()函数
Python中有split()和os.path.split()两个函数: split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表. os.path.split():将文件 ...
- python split(),os.path.split()和os.path.splitext()函数用法
https://blog.csdn.net/T1243_3/article/details/80170006 # -*- coding:utf-8 -*- """ @ ...
- python路径拼接os.path.join()函数的用法
os.path.join()函数:连接两个或更多的路径名组件 1.如果各组件名首字母不包含’/’,则函数会自动加上 2.如果有一个组件是一个绝对路径,则在它之前的所有组件均会被舍弃 3.如果最后一个组 ...
- glob.glob()、os.path.split()函数、global和nonlocal关键字
1. glob.glob() glob模块是Python最简单的模块之一, 内容非常少, 用它可以查找符合特定规则的文件路径名, 查找文件时只会用到三个匹配符: * :匹配0个或多个字符 ? : 匹配 ...
- python中 OS模块中 os.path.join() 函数用法简介
基础用法 os.path.join() 用于拼接文件的路径,可以传入多个待拼接的路径 若各个路径之间不存在 " / ", 则其会自动为各个路径之间增加连接符 " / &q ...
- python中使用os.path.join()
os.path.join的详细解释请移步os.path模块在使用的过程中,我使用如下代码: import ospath = "F:/gts/gtsdate/"b = os.path ...
- python中的os.path.dirname(__file__)的使用
在编程时,我们要获取当前文件所在的路径,以适合所有的工程,建立相对路径. python的os.path.dirname(__file__)非常好用,建议大家使用: import os FILE = o ...
随机推荐
- IOS 导航栏颜色 标题
修改导航栏颜 #define COLOR_TOMATO [UIColor colorWithRed:255/255.0f green:99/255.0f blue:71/255.0f alpha ...
- Xilinx COE文件格式小记
官方的参考文档是:https://www.xilinx.com/support/documentation/sw_manuals/xilinx11/cgn_r_coe_file_syntax.htm ...
- pyhton机器学习入门基础(机器学习与决策树)
//2019.07.26#scikit-learn数据挖掘工具包1.Scikit learn是基于python的数据挖掘和机器学习的工具包,方便实现数据的数据分析与高级操作,是数据分析里面非常重要的工 ...
- firewalld学习--维护命令
启动 systemctl start firewalld 停止 systemctl stop firewalld 重启 systemctl restart firewalld 查询状态 systemc ...
- 南邮平台之Hello,RE!
小白闲逛了一下南邮平台看到了逆向这题,小白在网上看了一下别人的write up发现有点复杂.于是小白就试试看,直接Underfine然后结果就出来了.....有点意外...... 结果flag{Wel ...
- RTL8711AM
官方文档主要修改 为了实现log服务 1,在log_service.h 取消注释 #ifndef LOG_SERVICE_H #define LOG_SERVICE_H #include " ...
- 《新标准C++程序设计》4.5(C++学习笔记15)
实例:长度可变的整型数组类 int main() { //要编写可变长整型数组类,使之能如下使用: CArray a; //开始里的数组是空的 ; i < ; ++i) a.push_back( ...
- 10 Class文件结构
- c# GlobalAddAtom GlobalDeleteAtom
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- SDRAM调试总结
SDRAM的调试总结 1 说明 实验平台: JZ2440 CPU: S3C2440 SDRAM型号: EM63A165TS-6G 2 SDRAM的一些基本概念 2.1 引脚分配 2.2 引脚描 ...