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") 
  
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''  
# 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  
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''      
# 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 '':
  • 12
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
python数据分析基础教程》 ⼀、导⼊常⽤numpy模块 from numpy import * //可以直接引⽤numpy的属性XXX import numpy as np //引⽤numpy的属性⼀定要np.XXX ⼆、常⽤函数以及转化关系 np.arange() 对应 python的range() np.array() 对应 python的list np.dtype() 对应 python的type() tolist()函数可以将numpy数组转换成python列表: 列表转为数组: warning:Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample. 这个warning主要就是有些函数参数应该是输⼊数组,当输⼊列表时就会警告!! 三、numpy数组操作函数 数组组合函数 将ndarray对象构成的元组作为参数输⼊ (1)⽔平组合:hstack((a,b)) 或者concatenate((a,b),axis=1) (2)垂直组合:vstack((a,b)) 或者concatenate((a,b),axis=0) (3)列组合:column((a,b)) (4)⾏组合:row_stack((a,b)) 数组的分割函数 (1)⽔平分割:hsplit(a,3) 或者 split(a,3,axis=1) (2)垂直分割:vsplit(a,3) 或者 split(a,3,axis=0) 四、⽂件处理——os库 1.os.system() 运⾏shell命令 2.os.listdir(path) 获得⽬录的内容 3.os.mkdir(path) 创建⽬录 4.os.rmdir(path) 删除⽬录 5.os.isdir(path) os.isfile(path) 判断是否为⽬录或者⽂件 6.os.remove(path) 删除⽂件 7.os.rename(old, new) 重命名⽂件或者⽬录 8.os.name 输出字符串指⽰正在使⽤的平台。如果是window 则⽤'nt'表⽰,对于Linux/Unix⽤户,它是'posix' 9.os.path.join() 在⽬录后⾯接上⽂件名 10.os.path.split() 返回⼀个路径的⽬录名和⽂件名 11.os.path.splitext() 分离⽂件名与扩展名 12.os.path.getsize(name) 获得⽂件⼤⼩,如果name是⽬录返回0L 14.os.path.abspath(")获得当前路径 15.os.path.dirname()返回⼀个路径的⽬录名 五、使⽤matplotlib画图(第九章 ) 前⾯⼏个列⼦主要讲解了通过多项式函数通过plt.plot()函数构建绘图,补充⼀下在机器学习散点绘制 import numpy as np import matplotlib.pyplot as plt fig=plt.figure() ax=fig.add_subplot(111) x1=[2, 2.6, 2.8] y1=[2, 2.4, 3] x2=[4,5 ,6] y2=[1.3, 2, 1.2] ax.scatter(x1,y1,s=20,c='red') ax.scatter(x2,y2,s=50,c='blue') plt.show() 另外:做数据分析——sklearn库 from sklearn import preprocessing 数据预处理:归⼀化、标准化、正则化处理 from sklearn import preprocessing preprocessing.normalize(features, norm='l2')//正则化
### 回答1: os.path.split() 函数Python 用于分割文件路径的函数。它接收一个字符串作为参数,返回一个元组,元组包含两个字符串:目录部分和文件名部分。如果传入的路径没有文件名部分,那么元组的第二个字符串为空字符串。 下面是 os.path.split() 函数用法示例: ```python import os path = '/path/to/file.txt' dir_path, file_name = os.path.split(path) print(dir_path) # 输出:/path/to print(file_name) # 输出:file.txt ``` 在这个例子os.path.split() 函数接收字符串 "/path/to/file.txt" 作为参数,然后将它分割成目录部分 "/path/to" 和文件名部分 "file.txt",并将它们存储在元组 dir_path 和 file_name 。最后,我们可以使用 print() 函数打印出这两个字符串。 ### 回答2: os.path.split()函数Python的内置函数之一,它被用来将一个文件路径分解为目录路径和文件名两个部分。该函数的返回值是一个元组 (path, filename),其 path 为文件所在目录的路径,filename 为文件的名称。 在使用os.path.split()函数时,只需要将文件的完整路径作为参数传递给该函数即可。例如: ```python import os path = '/path/to/file.txt' dir_path, file_name = os.path.split(path) print('Directory Path:', dir_path) print('File Name:', file_name) ``` 运行上述代码会输出如下结果: ``` Directory Path: /path/to File Name: file.txt ``` 上述代码,我们首先导入了os模块,然后定义了一个文件路径变量 path。接着,我们调用了os.path.split()函数,并将文件路径作为参数传递给该函数。然后,我们使用元组分解操作将返回值分别赋值给了两个变量:dir_path和file_name。最后,我们输出了这两个变量的值,以展示os.path.split()函数的使用方法。 需要注意的是,os.path.split()函数返回的路径名和文件名并不包含目录分隔符,例如斜杠(/)或反斜杠(\)。如果需要在返回的路径名后面添加目录分隔符,可以使用os.path.join()函数来拼接路径。 此外,os.path.split()函数还有一个“拓展名”的概念。如果路径包含拓展名,则os.path.split()会将其作为文件名的一部分返回,例如: ```python import os path = '/path/to/file.txt' dir_path, file_name = os.path.split(path) print('Directory Path:', dir_path) print('File Name (with extension):', file_name) ``` 输出结果: ``` Directory Path: /path/to File Name (with extension): file.txt ``` 如果希望只获取文件名,并去掉拓展名部分,可以使用os.path.splitext()函数,例如: ```python import os path = '/path/to/file.txt' file_name, extension = os.path.splitext(os.path.basename(path)) print('File Name (without extension):', file_name) print('Extension:', extension) ``` 输出结果: ``` File Name (without extension): file Extension: .txt ``` 以上就是关于os.path.split()函数用法的说明。该函数Python编写文件操作代码时非常有用,可以帮助我们方便地处理文件路径和文件名。 ### 回答3: os.path.split()函数Python的内置函数,用于分割一个路径字符串,返回一个包含路径和文件名的元组,其语法如下: os.path.split(path) 其path参数是需要分割的路径字符串。 该函数将路径字符串分割成两部分,分别是路径和文件名,返回值是一个元组,列表的第一个元素是路径,第二个元素是文件名。如果路径只包含文件名或为空,则返回的路径为一个空字符串,文件名为该路径。 例如,os.path.split("C:/Users/Administrator/Desktop/test.txt")返回的元组为("C:/Users/Administrator/Desktop", "test.txt")。 os.path.split()函数还可以使用多层嵌套来分割多层目录,例如: os.path.split("C:/Users/Administrator/Desktop/test/test2/file.txt") 返回的元组为("C:/Users/Administrator/Desktop/test/test2", "file.txt")。 在操作文件和目录时,该函数常常与os.path.join()函数搭配使用,以便更方便地进行路径字符串的操作。同时,也建议使用该函数进行路径字符串的分割,以免出现意外的Bug。 除此之外,os.path.split()函数还可以与os.path.splitext()函数搭配使用,将文件名分离出文件和扩展名,便于进行文件后缀名的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值