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修改字典内key对应值的方法
Jul 11 Python
浅谈python爬虫使用Selenium模拟浏览器行为
Feb 23 Python
使用Python进行QQ批量登录的实例代码
Jun 11 Python
pthon贪吃蛇游戏详细代码
Jan 27 Python
详解python校验SQL脚本命名规则
Mar 22 Python
Python批量生成幻影坦克图片实例代码
Jun 04 Python
python 实现GUI(图形用户界面)编程详解
Jul 17 Python
pygame实现贪吃蛇游戏(下)
Oct 29 Python
Python中pyecharts安装及安装失败的解决方法
Feb 18 Python
使用jupyter Nodebook查看函数或方法的参数以及使用情况
Apr 14 Python
python中逻辑与或(and、or)和按位与或异或(&、|、^)区别
Aug 05 Python
Python根据URL地址下载文件并保存至对应目录的实现
Nov 15 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中动态HTML的输出技术
2006/10/09 PHP
thinkphp制作404跳转页的简单实现方法
2016/09/22 PHP
JavaScript编程开发中的五个实用小技巧
2010/07/22 Javascript
JavaScript实现拼音排序的方法
2012/11/20 Javascript
jQuery实现强制cookie过期方法汇总
2015/05/22 Javascript
jQuery实现文本框输入同步的方法
2015/06/20 Javascript
Javascript实现数组中的元素上下移动
2017/04/28 Javascript
纯js实现页面返回顶部的动画(超简单)
2017/08/10 Javascript
angular 实时监听input框value值的变化触发函数方法
2018/08/31 Javascript
nodejs基础之buffer缓冲区用法分析
2018/12/26 NodeJs
jQuery实现获取当前鼠标位置并输出功能示例
2019/01/05 jQuery
JS实现提示框跟随鼠标移动
2019/08/27 Javascript
浅谈Layui的eleTree树式选择器使用方法
2019/09/25 Javascript
js实现录音上传功能
2019/11/22 Javascript
Vue性能优化的方法
2020/07/30 Javascript
python实现批量转换文件编码(批转换编码示例)
2014/01/23 Python
python实现字符串和日期相互转换的方法
2015/05/13 Python
利用Python获取操作系统信息实例
2016/09/02 Python
python自动登录12306并自动点击验证码完成登录的实现源代码
2018/04/25 Python
python 编写简单网页服务器的实例
2018/06/01 Python
详解python里的命名规范
2018/07/16 Python
python的turtle库使用详解
2019/05/10 Python
elasticsearch python 查询的两种方法
2019/08/04 Python
500行代码使用python写个微信小游戏飞机大战游戏
2019/10/16 Python
python使用梯度下降和牛顿法寻找Rosenbrock函数最小值实例
2020/04/02 Python
python3.6环境下安装freetype库和基本使用方法(推荐)
2020/05/10 Python
HTML5中的Web Notification桌面右下角通知功能的实现
2018/04/19 HTML / CSS
物理专业本科生自荐信
2014/01/30 职场文书
党校培训自我鉴定范文
2014/03/20 职场文书
安全协议书
2014/04/23 职场文书
片区教研活动总结
2014/07/02 职场文书
励志演讲稿500字
2014/08/21 职场文书
优秀教师先进个人事迹材料
2014/08/31 职场文书
报到证办理个人委托书
2014/10/06 职场文书
龙猫观后感
2015/06/09 职场文书
vue点击弹窗自动触发点击事件的解决办法(模拟场景)
2021/05/25 Vue.js