Python 文件重命名工具代码

所属分类: 脚本专栏 / python 阅读数: 949
收藏 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 Opencv实现单目标检测的示例代码

这篇文章主要介绍了Python Opencv实现单目标检测的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Python自动化之UnitTest框架实战记录

这篇文章主要给大家介绍了关于Python自动化之UnitTest框架实战的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

python如何将图片转换素描画

这篇文章主要介绍了python如何将图片转换素描画,帮助大家更好的用python处理图片,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解anaconda离线安装pytorchGPU版

这篇文章主要介绍了详解anaconda离线安装pytorchGPU版,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

python计算auc的方法

在本篇文章里小编给大家整理的是关于python计算auc的方法及相关内容,有兴趣的朋友们可以学习下。
收藏 0 赞 0 分享

python speech模块的使用方法

这篇文章主要介绍了python speech模块的使用方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python之语音识别speech模块

这篇文章主要介绍了python之语音识别speech模块,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

利用Python实现Json序列化库的方法步骤

这篇文章主要给大家介绍了关于利用Python实现Json序列化库的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

python 爬取B站原视频的实例代码

这篇文章主要介绍了python 爬取B站原视频的实例代码,帮助大家更好的理解和使用python 爬虫,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

python开发入门——set的使用

这篇文章主要介绍了python set的使用,帮助大家更好的理解和学习python,感兴趣的朋友可以了解下
收藏 0 赞 0 分享
查看更多