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和pyqt实现360的CLable控件
Feb 21 Python
零基础写python爬虫之抓取百度贴吧并存储到本地txt文件改进版
Nov 06 Python
对于Python编程中一些重用与缩减的建议
Apr 14 Python
Python黑魔法Descriptor描述符的实例解析
Jun 02 Python
使用Python进行目录的对比方法
Nov 01 Python
BP神经网络原理及Python实现代码
Dec 18 Python
Python time库基本使用方法分析
Dec 13 Python
Django 多对多字段的更新和插入数据实例
Mar 31 Python
利用python实现凯撒密码加解密功能
Mar 31 Python
python字典key不能是可以是啥类型
Aug 04 Python
五分钟带你搞懂python 迭代器与生成器
Aug 30 Python
Pytorch 统计模型参数量的操作 param.numel()
May 13 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防止sql注入示例分析和几种常见攻击正则表达式
2014/01/12 PHP
thinkphp3.2实现跨控制器调用其他模块的方法
2017/03/14 PHP
PHP实现的简单对称加密与解密方法实例小结
2017/08/28 PHP
extJs 文本框后面加上说明文字+下拉列表选中值后触发事件
2009/11/27 Javascript
jQuery 操作option的实现代码
2011/03/03 Javascript
让textarea自动调整大小的js代码
2011/04/12 Javascript
如何让easyui gridview 宽度自适应窗口改变及fitColumns应用
2013/01/25 Javascript
js实现连个数字相加而不是拼接的方法
2014/02/23 Javascript
JavaScript对数字的判断与处理实例分析
2015/02/02 Javascript
JS+CSS实现简单滑动门(滑动菜单)效果
2015/09/19 Javascript
jQuery Easyui使用(一)之可折叠面板的布局手风琴菜单
2016/08/17 Javascript
Bootstrap基本插件学习笔记之轮播幻灯片(23)
2016/12/08 Javascript
JS 实现 ajax 异步浏览器兼容问题
2017/01/21 Javascript
微信小程序实现滑动删除效果
2017/05/19 Javascript
JavaScript之Date_动力节点Java学院整理
2017/06/28 Javascript
前端跨域的几种解决方式总结(推荐)
2017/08/16 Javascript
NodeJS父进程与子进程资源共享原理与实现方法
2018/03/16 NodeJs
详解vue在项目中使用百度地图
2019/03/26 Javascript
浅析Python中的join()方法的使用
2015/05/19 Python
Python使用Scrapy爬取妹子图
2015/05/28 Python
Python去除、替换字符串空格的处理方法
2018/04/01 Python
python实现log日志的示例代码
2018/04/28 Python
Python Selenium操作Cookie的实例方法
2021/02/28 Python
全球知名的珠宝首饰品牌:Kay Jewelers
2018/02/11 全球购物
Clarks其乐鞋荷兰官网:Clarks荷兰
2019/07/05 全球购物
C++是不是类型安全的
2014/02/18 面试题
个人实习生的自我评价
2014/02/16 职场文书
关于安全的标语
2014/06/10 职场文书
交通违章检讨书
2014/09/21 职场文书
服务承诺书
2015/01/19 职场文书
认真学习保证书
2015/02/26 职场文书
2015年党总支工作总结
2015/05/25 职场文书
2015年暑期实践报告范文
2015/07/13 职场文书
党组织关系的介绍信模板
2019/06/21 职场文书
Java Socket实现Redis客户端的详细说明
2021/05/26 Redis
微信小程序scroll-view不能左右滑动问题的解决方法
2021/07/09 Javascript