Python 文件重命名工具代码


Posted in Python onJuly 26, 2009
#Filename:brn.py 
#Description: batch replace certain words in file names 
#Use to bat rename the file in a dir(modify the suffix from a to b) for Windows Vista OS 
import sys 
import os 
import fnmatch 
import re 
#parse params 
p=input("Please input work directory(current path for enter):") 
if p=='\r': 
p='.' 
p=p.rstrip('\r') 
print (p) 
while not os.path.exists(p): 
print (p+' is not existed.Please input the work directory:') 
p=input("Please input work directory(current path for enter):") 
s=input("Please enter the words which need be modified(must):") 
while s=='\r': 
s=input("Please enter the words which need be replaced(must):") 
s=s.rstrip('\r') 
d=input("Please enter the words which want to change to(must):") 
while d=='\r': 
d=input("Please enter the words which want to change to(must):") 
d=d.rstrip('\r') 
try: 
sure=input("Are you sure to rename the file named *"+s+"*"+" to *"+d+"*"+" in directory "+p+"? y/n:") 
sure=sure.rstrip('\r') 
if sure!='y': 
print ("Cancel") 
else: 
for root, dirs, files in os.walk(p, True): 
for file in files: 
print (os.path.join(root,file)) 
if os.path.isfile(os.path.join(root,file)):#Only file is file,not a dir ,do this 
if fnmatch.fnmatch(file, '*'+s+'*'): 
f=str(file).replace(s,d) 
if p=='.': 
command='move '+str(file)+" "+f 
else: 
command="move "+os.path.join(root,file)+" "+os.path.join(root,f) 
print (command) 
if os.system(command)==0:#do actual rename 
print ("Rename "+str(file)+" to "+f+" success") 
else: 
print ("Rename "+str(file)+" to "+f+" failed") 
#else: 
#print str(file)+" is a directory.omit" 
except IndexError: 
print (IndexError.message)
Python 相关文章推荐
Python ORM框架SQLAlchemy学习笔记之数据添加和事务回滚介绍
Jun 10 Python
Python装饰器decorator用法实例
Nov 10 Python
python修改字典内key对应值的方法
Jul 11 Python
每天迁移MySQL历史数据到历史库Python脚本
Apr 13 Python
Python贪心算法实例小结
Apr 22 Python
Python中判断输入是否为数字的实现代码
May 26 Python
对Python实现简单的API接口实例讲解
Dec 10 Python
python xpath获取页面注释的方法
Jan 14 Python
Python实现的银行系统模拟程序完整案例
Apr 12 Python
Python + Requests + Unittest接口自动化测试实例分析
Dec 12 Python
Python机器学习算法之决策树算法的实现与优缺点
May 13 Python
关于python pygame游戏进行声音添加的技巧
Oct 24 Python
python 生成目录树及显示文件大小的代码
Jul 23 #Python
python 域名分析工具实现代码
Jul 15 #Python
python 自动提交和抓取网页
Jul 13 #Python
python self,cls,decorator的理解
Jul 13 #Python
python 解析html之BeautifulSoup
Jul 07 #Python
打印出python 当前全局变量和入口参数的所有属性
Jul 01 #Python
python 查找文件夹下所有文件 实现代码
Jul 01 #Python
You might like
PHP入门速成(2)
2006/10/09 PHP
php smarty模版引擎中的缓存应用
2009/12/02 PHP
又一个php 分页类实现代码
2009/12/03 PHP
PHP实现ASCII码与字符串相互转换的方法
2017/04/29 PHP
php+ajax实现无刷新文件上传功能(ajaxuploadfile)
2018/02/11 PHP
PHP PDOStatement::rowCount讲解
2019/02/01 PHP
JS 容错处理代码, 屏蔽错误信息
2021/03/09 Javascript
List Installed Hot Fixes
2007/06/12 Javascript
JavaScript 入门·JavaScript 具有全范围的运算符
2007/10/01 Javascript
javascript URL编码和解码使用说明
2010/04/12 Javascript
跨浏览器的 mouseenter mouseleave 以及 compareDocumentPosition的使用说明
2010/05/04 Javascript
Javascript合并表格中具有相同内容单元格示例
2013/08/11 Javascript
Iframe实现跨浏览器自适应高度解决方法
2014/09/02 Javascript
Jquery左右滑动插件之实现超级炫酷动画效果附源码下载
2015/12/02 Javascript
JS原型、原型链深入理解
2016/02/27 Javascript
简单理解JavaScript中的封装与继承特性
2016/03/19 Javascript
JS获取IMG图片高宽的简单实例
2016/05/17 Javascript
js实现楼层效果的简单实例
2016/07/15 Javascript
BootStrap Validator使用注意事项(必看篇)
2016/09/28 Javascript
Vue实现表格中对数据进行转换、处理的方法
2018/09/06 Javascript
Vue源码中要const _toStr = Object.prototype.toString的原因分析
2018/12/09 Javascript
JS FormData对象使用方法实例详解
2020/02/12 Javascript
Python文件操作类操作实例详解
2014/07/11 Python
Django的HttpRequest和HttpResponse对象详解
2018/01/26 Python
python函数定义和调用过程详解
2020/02/09 Python
pytho matplotlib工具栏源码探析一之禁用工具栏、默认工具栏和工具栏管理器三种模式的差异
2021/02/25 Python
利用Bootstrap实现漂亮简洁的CSS3价格表实例源码
2017/03/02 HTML / CSS
英国第二大营养品供应商:Vitabiotics
2016/10/01 全球购物
一套软件测试笔试题
2014/07/25 面试题
自考生毕业自我鉴定
2013/10/10 职场文书
棉花姑娘教学反思
2014/02/15 职场文书
我的梦中国梦演讲稿
2014/04/23 职场文书
三月学雷锋月活动总结
2014/04/28 职场文书
培训通知
2015/04/17 职场文书
小学三年级班主任工作经验交流材料
2015/11/02 职场文书
浅析Python OpenCV三种滤镜效果
2022/04/11 Python