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中动态获取对象的属性和方法的教程
Apr 09 Python
十个Python程序员易犯的错误
Dec 15 Python
Pandas 合并多个Dataframe(merge,concat)的方法
Jun 08 Python
Python3实现的判断环形链表算法示例
Mar 07 Python
python判断文件夹内是否存在指定后缀文件的实例
Jun 10 Python
python写程序统计词频的方法
Jul 29 Python
tensorflow实现在函数中用tf.Print输出中间值
Jan 21 Python
pytorch 实现在一个优化器中设置多个网络参数的例子
Feb 20 Python
python实现提取COCO,VOC数据集中特定的类
Mar 10 Python
python实现图片转换成素描和漫画格式
Aug 19 Python
Python 字典一个键对应多个值的方法
Sep 29 Python
python实现简单聊天功能
Jul 07 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 Undefined index的问题
2009/06/01 PHP
phpmyadmin配置文件现在需要绝密的短密码(blowfish_secret)的2种解决方法
2014/05/07 PHP
将CMYK颜色值和RGB颜色相互转换的PHP代码
2014/07/28 PHP
PHP模拟登陆163邮箱发邮件及获取通讯录列表的方法
2015/03/07 PHP
PHP获取音频文件的相关信息
2015/06/22 PHP
自制PHP框架之路由与控制器
2017/05/07 PHP
[原创]来自ImageSee官方 JavaScript图片浏览器
2008/01/16 Javascript
JavaScript入门教程(3) js面向对象
2009/01/31 Javascript
jquery如何改变html标签的样式(两种实现方法)
2013/01/16 Javascript
JS教程:window.location使用方法的区别介绍
2013/10/04 Javascript
jquery插件jquery倒计时插件分享
2013/12/27 Javascript
Javascript中this关键字的一些小知识
2015/03/15 Javascript
JavaScript实现简单图片翻转的方法
2015/04/17 Javascript
javascript实现很浪漫的气泡冒出特效
2020/09/05 Javascript
JS工作中的小贴士之”闭包“与事件委托的”阻止冒泡“
2016/06/16 Javascript
Javascript闭包与函数柯里化浅析
2016/06/22 Javascript
AngularJS入门教程之Helloworld示例
2016/12/25 Javascript
浅谈JavaScript中的apply/call/bind和this的使用
2017/02/26 Javascript
Easyui ueditor 整合解决不能编辑的问题(推荐)
2017/06/25 Javascript
vue router 跳转时打开新页面的示例方法
2019/07/28 Javascript
基于iview-admin实现动态路由的示例代码
2019/10/02 Javascript
VUE:vuex 用户登录信息的数据写入与获取方式
2019/11/11 Javascript
JS中FileReader类实现文件上传及时预览功能
2020/03/27 Javascript
Vue 中获取当前时间并实时刷新的实现代码
2020/05/12 Javascript
如何利用 JS 脚本实现网页全自动秒杀抢购功能
2020/10/12 Javascript
Python sys.path详细介绍
2013/10/17 Python
python检查字符串是否是正确ISBN的方法
2015/07/11 Python
python的scikit-learn将特征转成one-hot特征的方法
2018/07/10 Python
python如何删除文件中重复的字段
2019/07/16 Python
Python3操作YAML文件格式方法解析
2020/04/10 Python
Python实例方法、类方法、静态方法区别详解
2020/09/05 Python
5.1手机促销活动
2014/01/17 职场文书
女方离婚起诉书
2015/05/18 职场文书
2019大学竞选班长发言稿
2019/06/27 职场文书
Nginx配置https的实现
2021/11/27 Servers
python数字图像处理:图像简单滤波
2022/06/28 Python