python剔除文件夹中某个格式,并重命名剩余的图片

该脚本用于处理CMU大学的图片数据集,包括重命名文件夹和图片,删除特定类型的文件如coord.dat、Document.txt等,并将图片统一命名格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

filename = r'D:\Work\process_cmu\cmu数据分析\cmu_img_all\\'
delet_name = ['coord.dat','Document.txt','spectrtuning.m', 'Thumbs.db']
for i in range(1, 49):
    base_dir = filename + 'p' + str(i) + '\\'
#     print(len(os.listdir(base_dir)))
    for j, k in zip(os.listdir(base_dir),range(1, len(os.listdir(base_dir)) + 1)):
        os.rename(base_dir + j, base_dir + 'c' + str(k))
        second_path = base_dir + 'c' + str(k) + '\\'
        
        if os.path.exists(second_path + 'coord.dat'):
            os.remove(second_path + 'coord.dat')
        if os.path.exists(second_path + 'Document.txt'):
            os.remove(second_path + 'Document.txt')
        if os.path.exists(second_path + 'spectrtuning.m'):
            os.remove(second_path + 'spectrtuning.m')
        if os.path.exists(second_path + 'Thumbs.db'):
            os.remove(second_path + 'Thumbs.db')
            
        for img, n in zip(os.listdir(second_path),range(1,66)):
            os.rename(second_path + img, second_path + 's' + str(n) + '_p' + str(i) + '_c' + str(k) + '.bmp')

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值