Python 文件重命名工具代码

所属分类: 脚本专栏 / python 阅读数: 955
收藏 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 OrderedDict的使用案例解析

这篇文章主要介绍了Python OrderedDict的使用案例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python计算n的阶乘的方法代码

在本篇文章里小编给各位整理的是关于python计算n的阶乘的相关知识点,需要的朋友们参考下。
收藏 0 赞 0 分享

DJANGO-URL反向解析REVERSE实例讲解

在本篇文章里小编给大家整理的是一篇关于DJANGO-URL反向解析REVERSE的相关知识点内容,需要的朋友们学习下。
收藏 0 赞 0 分享

python实现淘宝购物系统

这篇文章主要为大家详细介绍了python实现简易的淘宝购物系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Python aiohttp百万并发极限测试实例分析

这篇文章主要介绍了Python aiohttp百万并发极限测试,结合实例形式分析了Python异步编程基于aiohttp客户端高并发请求的相关操作技巧与使用注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

python单例模式原理与创建方法实例分析

这篇文章主要介绍了python单例模式原理与创建方法,结合实例形式分析了Python单例模式的概念、原理、定义、使用方法及相关操作注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

python图的深度优先和广度优先算法实例分析

这篇文章主要介绍了python图的深度优先和广度优先算法,结合实例形式分析了图的深度优先算法与广度优先算法相关概念、原理、实现技巧与操作注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

python实现树的深度优先遍历与广度优先遍历详解

这篇文章主要介绍了python实现树的深度优先遍历与广度优先遍历,详细分析了树的深度优先遍历与广度优先遍历原理及Python相关实现技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

通过字符串导入 Python 模块的方法详解

这篇文章主要介绍了通过字符串导入 Python 模块的方法详解,本文通过实例结合,给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python用类实现文章敏感词的过滤方法示例

这篇文章主要介绍了python用类实现文章敏感词的过滤方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享
查看更多