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带你玩转冲顶大会
Jan 17 Python
Django 1.10以上版本 url 配置注意事项详解
Aug 05 Python
python脚本调用iftop 统计业务应用流量的思路详解
Oct 11 Python
Python如何基于smtplib发不同格式的邮件
Dec 30 Python
使用Tensorboard工具查看Loss损失率
Feb 15 Python
python数据库编程 ODBC方式实现通讯录
Mar 27 Python
Python内置方法和属性应用:反射和单例(推荐)
Jun 19 Python
python对execl 处理操作代码
Jun 22 Python
PyQt中使用QtSql连接MySql数据库的方法
Jul 28 Python
pycharm如何使用anaconda中的各种包(操作步骤)
Jul 31 Python
解决python 在for循环并且pop数组的时候会跳过某些元素的问题
Dec 11 Python
Python 利用argparse模块实现脚本命令行参数解析
Dec 28 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
MySQL连接数超过限制的解决方法
2011/07/17 PHP
基于php socket(fsockopen)的应用实例分析
2013/06/02 PHP
浅析使用Turck-mmcache编译来加速、优化PHP代码
2013/06/20 PHP
php微信公众开发之获取周边酒店信息的方法
2014/12/22 PHP
Laravel执行migrate命令提示:No such file or directory的解决方法
2016/03/16 PHP
Yii2 ActiveRecord多表关联及多表关联搜索的实现
2016/06/30 PHP
php生出随机字符串
2017/07/06 PHP
PHP基于迭代实现文件夹复制、删除、查看大小等操作的方法
2017/08/11 PHP
thinkphp5.1框架容器与依赖注入实例分析
2019/07/23 PHP
JavaScript访问样式表代码
2010/10/15 Javascript
Javascript 自适应高度的Tab选项卡
2011/04/05 Javascript
jsonp原理及使用
2013/10/28 Javascript
把多个JavaScript函数绑定到onload事件处理函数上的方法
2016/09/04 Javascript
Vuejs第一篇之入门教程详解(单向绑定、双向绑定、列表渲染、响应函数)
2016/09/09 Javascript
微信小程序实现给循环列表添加点击样式实例
2017/04/26 Javascript
JavaScript实现三级联动效果
2017/07/15 Javascript
webpack使用 babel-loader 转换 ES6代码示例
2017/08/21 Javascript
浅谈vue父子组件怎么传值
2018/07/21 Javascript
ES6 如何改变JS内置行为的代理与反射
2019/02/11 Javascript
JS/CSS实现字符串单词首字母大写功能
2019/09/03 Javascript
vant IndexBar实现的城市列表的示例代码
2019/11/20 Javascript
微信小程序scroll-view隐藏滚动条的方法详解
2020/03/25 Javascript
VSCode 配置uni-app的方法
2020/07/11 Javascript
Python编写Windows Service服务程序
2018/01/04 Python
python中pylint使用方法(pylint代码检查)
2018/04/06 Python
flask入门之表单的实现
2018/07/18 Python
在python里协程使用同步锁Lock的实例
2019/02/19 Python
python和mysql交互操作实例详解【基于pymysql库】
2019/06/04 Python
对Python强大的可变参数传递机制详解
2019/06/13 Python
tensorflow实现将ckpt转pb文件的方法
2020/04/22 Python
苏格兰在线威士忌商店:The Whisky Barrel
2019/05/07 全球购物
Dr. Martens马汀博士德国官网:马丁靴鼻祖
2019/12/26 全球购物
个人找工作求职简历的自我评价
2013/10/20 职场文书
大一新生学期自我评价
2014/04/09 职场文书
大学迎新标语
2014/06/26 职场文书
zabbix监控mysql的实例方法
2021/06/02 MySQL