Python 文件重命名工具代码

所属分类: 脚本专栏 / python 阅读数: 930
收藏 0 赞 0 分享
复制代码 代码如下:

#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实现服务器中只重载被修改的进程的方法,包括用watchdog来检测文件的变化等,实现起来充分体现了Python作为动态语言的灵活性,强烈推荐!需要的朋友可以参考下
收藏 0 赞 0 分享

python使用mailbox打印电子邮件的方法

这篇文章主要介绍了python使用mailbox打印电子邮件的方法,涉及Python打印电子邮件的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

python中随机函数random用法实例

这篇文章主要介绍了python中随机函数random用法,实例分析了random函数的相关使用技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python模拟enum枚举类型的方法小结

这篇文章主要介绍了python模拟enum枚举类型的方法,实例总结了python模拟enum枚举类型的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python遍历数组的方法小结

这篇文章主要介绍了python遍历数组的方法,实例总结了两种Python遍历数组的技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python实现指定字符串补全空格的方法

这篇文章主要介绍了python实现指定字符串补全空格的方法,涉及Python中rjust,ljust和center方法的使用技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python实现同时给多个变量赋值的方法

这篇文章主要介绍了python实现同时给多个变量赋值的方法,涉及Python中变量赋值的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python中assert用法实例分析

这篇文章主要介绍了python中assert用法,实例分析了assert的功能及相关使用技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python中pass语句用法实例分析

这篇文章主要介绍了python中pass语句用法,对比C++程序实例分析了pass语句的使用方法,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python实现的简单文本类游戏实例

这篇文章主要介绍了python实现的简单文本类游戏,以两个实例形式分析了python操作文本与字符串的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多