分离文件名与扩展名
# 分离文件名与扩展名:os.path.splitext(path)
# 默认返回(fname, fextension)元组
path_01 = 'E:STHFoobar2000install.log'
path_02 = 'E:STHFoobar2000'
res_01 = os.path.splitext(path_01)
res_02 = os.path.splitext(path_02)
print(res_01)
print(res_02)
('E:\STH\Foobar2000\install', '.log')
('E:\STH\Foobar2000', '')
import os
from pprint import pprint
filelist = ['IMG_20131222_100902.jpg', 'IMG_20131222_100917.jpg', 'IMG_20131222_101052.jpg', 'IMG_20131222_101136.jpg']
pprint(filelist)
for index, img in enumerate(filelist):
name = str(index + 1).zfill(6) # 字符串填充
ext = os.path.splitext(img)[-1] # 分离文件名与扩展名
print(f'new name -- {name}{ext}')
# os.rename(old, new)
['IMG_20131222_100902.jpg',
'IMG_20131222_100917.jpg',
'IMG_20131222_101052.jpg',
'IMG_20131222_101136.jpg']
new name -- 000001.jpg
new name -- 000002.jpg
new name -- 000003.jpg
new name -- 000004.jpg