转自:
https://www.cnblogs.com/liangmingshen/p/10215065.html
用法
os.path.splitext(“文件路径”) 分离文件名与扩展名;默认返回(fname,fextension)元组,可做分片操作
例子
import os
path_01='E:\STH\Foobar2000\install.log'
path_02='E:\STH\Foobar2000'
res_01=os.path.splitext(path_01)
res_02=os.path.splitext(path_02)
print(root_01)
print(root_02)
结果
('E:\\STH\\Foobar2000\\install', '.log')
('E:\\STH\\Foobar2000', '')
类似做法
import os
need_find_path = '/home/ys/Desktop/png_jpg'
count = 0
for root,dirs,files in os.walk(need_find_path): # 遍历当前目录的文件夹、文件
for file in files: # file内容:0011bbdb3ec204e90639492cca028f292801fae9448a730b800908b3.jpg
a = file.split(".")[1]
a = file.find(".")[1]
a = a.lower() # 将字符串转化为小写
print(a) # 输出jpg,注意,这里没有点