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 相关文章推荐
PHP网页抓取之抓取百度贴吧邮箱数据代码分享
Apr 13 Python
Python中xrange与yield的用法实例分析
Dec 26 Python
Python操作mongodb的9个步骤
Jun 04 Python
Python闭包函数定义与用法分析
Jul 20 Python
对python 匹配字符串开头和结尾的方法详解
Oct 27 Python
python保存字典和读取字典的实例代码
Jul 07 Python
python读写csv文件并增加行列的实例代码
Aug 01 Python
使用Python paramiko模块利用多线程实现ssh并发执行操作
Dec 05 Python
解决jupyter notebook 出现In[*]的问题
Apr 13 Python
使用python matplotlib 画图导入到word中如何保证分辨率
Apr 16 Python
Python实现列表索引批量删除的5种方法
Nov 16 Python
python中requests模拟登录的三种方式(携带cookie/session进行请求网站)
Nov 17 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实现加密的几种方式介绍
2015/02/22 PHP
PHP实现的浏览器检查类
2016/04/11 PHP
微信公众平台开发教程①获取用户Openid及个人信息图文详解
2019/04/10 PHP
jquery 插件之仿“卓越亚马逊”首页弹出菜单效果
2008/12/25 Javascript
JavaScript实现网页图片等比例缩放实现代码及调用方式
2013/02/25 Javascript
解析js如何获取当前url中的参数值并复制给input
2013/06/23 Javascript
Javascript 按位左移运算符使用介绍(
2014/02/04 Javascript
node.js中的fs.chmod方法使用说明
2014/12/18 Javascript
浅谈jQuery中的事件
2015/03/23 Javascript
js实现的二级横向菜单条实例
2015/08/22 Javascript
JavaScript判断按钮被点击的方法
2015/12/13 Javascript
浅谈Node.js 子进程与应用场景
2018/01/24 Javascript
详解vuex中mapState,mapGetters,mapMutations,mapActions的作用
2018/04/13 Javascript
NodeJS模块与ES6模块系统语法及注意点详解
2019/01/04 NodeJs
详解VScode编辑器vue环境搭建所遇问题解决方案
2019/04/26 Javascript
Vue的props父传子的示例代码
2020/05/20 Javascript
JavaScript undefined及null区别实例解析
2020/07/21 Javascript
python入门之语句(if语句、while语句、for语句)
2015/01/19 Python
python安装以及IDE的配置教程
2015/04/29 Python
Python实现监控程序执行时间并将其写入日志的方法
2015/06/30 Python
python连接mysql实例分享
2016/10/09 Python
Python内置加密模块用法解析
2019/11/25 Python
在Tensorflow中实现leakyRelu操作详解(高效)
2020/06/30 Python
纯CSS3实现移动端展开和收起效果的示例代码
2020/04/26 HTML / CSS
北京振戎融通Java面试题
2015/09/03 面试题
php优化查询foreach代码实例讲解
2021/03/24 PHP
简历中个人求职的自我评价模板
2013/11/29 职场文书
机关财务管理制度
2014/01/17 职场文书
面试后的英文感谢信
2014/02/01 职场文书
技校毕业生自荐信
2014/06/03 职场文书
反四风对照检查材料思想汇报
2014/09/16 职场文书
公司的力量观后感
2015/06/05 职场文书
2015年学校少先队工作总结
2015/07/20 职场文书
保外就医申请书范文
2015/08/06 职场文书
2016年七夕情人节宣传语
2015/11/25 职场文书
MySQL数据库查询进阶之多表查询详解
2022/04/08 MySQL