os.path.splitext():将文件名和扩展名分开
os.path.split() :返回文件的路径和文件名
1 #os.path.splitext()将文件名和扩展名分开 2 fname,fename=os.path.splitext('E:/2020年博一上课/新建文本文档.txt') 3 print ('fname is:',fname) 4 print ('fename is:',fename) 5 6 fname is: E:/2020年博一上课/新建文本文档 7 fename is: .txt
1 #os.path.split()返回文件的路径和文件名 2 dirname,filename=os.path.split('E:/2020年博一上课/新建文本文档.txt') 3 print (dirname) 4 print (filename) 5 6 E:/2020年博一上课 7 新建文本文档.txt
1 import os 2 3 path_01='E:STHFoobar2000install.log' 4 path_02='E:STHFoobar2000' 5 res_01=os.path.splitext(path_01) 6 res_02=os.path.splitext(path_02) 7 res_03=os.path.split(path_02) 8 print(res_01) 9 print(res_02) 10 print(res_03) 11 12 13 ('E:\STH\Foobar2000\install', '.log') 14 ('E:\STH\Foobar2000', '') 15 ('E:\STH', 'Foobar2000')
参考:https://blog.csdn.net/ztf312/article/details/83039392 https://www.cnblogs.com/liangmingshen/p/10215065.html