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实现TCP协议下的端口映射功能的脚本程序示例
Jun 14 Python
python基础学习之如何对元组各个元素进行命名详解
Jul 12 Python
Django csrf 验证问题的实现
Oct 09 Python
Python 实现平台类游戏添加跳跃功能
Mar 27 Python
keras用auc做metrics以及早停实例
Jul 02 Python
python 怎样进行内存管理
Nov 10 Python
Python将list元素转存为CSV文件的实现
Nov 16 Python
python录音并调用百度语音识别接口的示例
Dec 01 Python
python 数据类型强制转换的总结
Jan 25 Python
matplotlib相关系统目录获取方式小结
Feb 03 Python
python实现经典排序算法的示例代码
Feb 07 Python
Python进程间的通信之语法学习
Apr 11 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 数组遍历的差异(array_diff 的实现)
2008/03/23 PHP
深入探讨PHP中的内存管理问题
2011/08/31 PHP
9个实用的PHP代码片段分享
2015/01/22 PHP
如何在HTML 中嵌入 PHP 代码
2015/05/13 PHP
php 类自动载入的方法
2015/06/03 PHP
PHP基于rabbitmq操作类的生产者和消费者功能示例
2018/06/16 PHP
PHP+MySQL实现在线测试答题实例
2020/01/02 PHP
javascript基础知识大集锦(一) 推荐收藏
2011/01/13 Javascript
分享一个asp.net pager分页控件
2012/01/04 Javascript
jQuery 下拉列表 二级联动插件分享
2012/03/29 Javascript
利用浏览器全屏api实现js全屏
2014/01/16 Javascript
使用Node.js配合Nginx实现高负载网络
2015/06/28 Javascript
jQuery图片轮播滚动切换代码分享
2020/04/20 Javascript
设计模式中的facade外观模式在JavaScript开发中的运用
2016/05/18 Javascript
Node.js Express 框架 POST方法详解
2017/01/23 Javascript
Angularjs 事件指令详细整理
2017/07/27 Javascript
jQuery 实现鼠标画框并对框内数据选中的实例代码
2017/08/29 jQuery
浅谈Vue Element中Select下拉框选取值的问题
2018/03/01 Javascript
node上的redis调用优化示例详解
2018/10/30 Javascript
React精髓!一篇全概括小结(急速)
2019/05/23 Javascript
解决vue页面刷新,数据丢失的问题
2020/11/24 Vue.js
详解Python中的__new__()方法的使用
2015/04/09 Python
深入探究Python中变量的拷贝和作用域问题
2015/05/05 Python
Python新手们容易犯的几个错误总结
2017/04/01 Python
Windows10下 python3.7 安装 facenet的教程
2019/09/10 Python
python zip()函数的使用示例
2020/09/23 Python
python查询MySQL将数据写入Excel
2020/10/29 Python
html5实现滑块功能之type="range"属性
2020/02/18 HTML / CSS
Debenhams百货英国官方网站:Debenhams UK
2016/07/12 全球购物
儿科护理实习自我鉴定
2013/09/19 职场文书
消防宣传口号
2014/06/16 职场文书
2015年幼儿园后勤工作总结
2015/04/25 职场文书
红楼梦读书笔记
2015/06/25 职场文书
培训计划通知
2015/07/15 职场文书
pytorch查看网络参数显存占用量等操作
2021/05/12 Python
如何开启Apache,Nginx和IIS服务器的GZIP压缩功能
2022/04/29 Servers