利用 python 对文件夹下图片数据进行批量改名
2016-06-30 10:36
567 查看
1. 前言
我们最近在做一个使用flask 模拟 instagram 的图片分享网站, 需要一些基本的图片数据, 我们这里采用的是本地提供, 但是, 使用 爬虫从网上爬下来的图片, 名字都是乱七八糟的, 不利于编程, 这里就需要对他们进行批量改名操作。2. 基本思路
使用python 的os 模块, 对文件夹进行遍历(listdir), 同时使用 rename 进行改名操作3. 实现效果
4. 实现代码
代码非常简单# -*- coding:utf8 -*- import os class BatchRename(): ''' 批量重命名文件夹中的图片文件 ''' def __init__(self): self.path = 'C:/Users/ThinkPad User/Desktop/weibo' def rename(self): filelist = os.listdir(self.path) total_num = len(filelist) i = 0 for item in filelist: if item.endswith('.jpg'): src = os.path.join(os.path.abspath(self.path), item) dst = os.path.join(os.path.abspath(self.path), str(i) + '.jpg') try: os.rename(src, dst) print 'converting %s to %s ...' % (src, dst) i = i + 1 except: continue print 'total %d to rename & converted %d jpgs' % (total_num, i) if __name__ == '__main__': demo = BatchRename() demo.rename()
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法