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多进程编程
Jun 12 Python
浅谈python内置变量-reversed(seq)
Jun 21 Python
Django 生成登陆验证码代码分享
Dec 12 Python
学习Python selenium自动化网页抓取器
Jan 20 Python
利用Pycharm断点调试Python程序的方法
Nov 29 Python
Python使用pyserial进行串口通信的实例
Jul 02 Python
解决Python正则表达式匹配反斜杠''\''问题
Jul 17 Python
简单分析python的类变量、实例变量
Aug 23 Python
使用python切片实现二维数组复制示例
Nov 26 Python
tensorflow通过模型文件,使用tensorboard查看其模型图Graph方式
Jan 23 Python
python实现同一局域网下传输图片
Mar 20 Python
OpenCV灰度化之后图片为绿色的解决
Dec 01 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直接修改表内容DataGrid功能实现代码
2015/09/24 PHP
thinkPHP3.2.3实现阿里大于短信验证的方法
2018/06/06 PHP
基于jQuery实现select下拉选择可输入附源码下载
2016/02/03 Javascript
基于jQuery实现Ajax验证用户名是否存在实例
2016/03/30 Javascript
深入浅出讲解ES6的解构
2016/08/03 Javascript
CSS3 3D 技术手把手教你玩转
2016/09/02 Javascript
浅析Jquery操作select
2016/12/13 Javascript
javascript数据类型详解
2017/02/07 Javascript
js如何获取网页所有图片
2017/05/12 Javascript
Js自定义多选框效果的实例代码
2017/07/05 Javascript
vue实现消息的无缝滚动效果的示例代码
2017/12/05 Javascript
实例解析Vue.js下载方式及基本概念
2018/05/11 Javascript
layui实现数据表格自定义数据项
2019/10/26 Javascript
javascript实现移动端上传图片功能
2020/08/18 Javascript
[01:08]DOTA2次级职业联赛 - Wings 战队宣传片
2014/12/01 DOTA
python持久性管理pickle模块详细介绍
2015/02/18 Python
Python中一些自然语言工具的使用的入门教程
2015/04/13 Python
在Mac OS上使用mod_wsgi连接Python与Apache服务器
2015/12/24 Python
对django中render()与render_to_response()的区别详解
2018/10/16 Python
对python数据切割归并算法的实例讲解
2018/12/12 Python
Django JWT Token RestfulAPI用户认证详解
2019/01/23 Python
python获取依赖包和安装依赖包教程
2020/02/13 Python
django filter过滤器实现显示某个类型指定字段不同值方式
2020/07/16 Python
html5中audio支持音频格式的解决方法
2018/08/24 HTML / CSS
用canvas画心电图的示例代码
2018/09/10 HTML / CSS
AmazeUI 图标的示例代码
2020/08/13 HTML / CSS
欧洲最大的美妆零售网站:Feelunique
2017/01/14 全球购物
师范应届生语文教师求职信
2013/10/29 职场文书
师范生个人推荐信
2013/11/29 职场文书
小区门卫工作职责
2013/12/14 职场文书
财务管理职业生涯规划书
2014/02/26 职场文书
四风对照检查材料范文
2014/09/27 职场文书
安全生产工作汇报
2014/10/28 职场文书
先进个人总结范文
2015/02/15 职场文书
春节晚会开场白
2015/05/29 职场文书
Apache Calcite 实现方言转换的代码
2021/04/24 Servers