Python 文件重命名工具代码
时间:2022-09-25 05:15:32
#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,文件重命名
0
投稿
猜你喜欢
Python 图片转数组,二进制互转操作
2023-10-07 17:46:04
加固SQL参数与存储过程
2012-03-12 19:44:08
彻底弄懂Python中的回调函数(callback)
2021-02-26 23:51:57
python使用selenium模拟浏览器进入好友QQ空间留言功能
2021-06-24 16:24:16
mysql8.0.23 msi安装超详细教程
2024-01-22 13:44:14
很全面的Mysql数据库、数据库表、数据基础操作笔记(含代码)
2024-01-15 11:19:42
Python Playwright 文本框操作技巧
2023-01-15 20:59:21
ASP网站数据采集经验谈
2008-03-09 15:30:00
vue打包npm run build时候界面报错的解决
2024-05-10 14:18:52
python用线性回归预测股票价格的实现代码
2023-01-24 02:14:31
@ResponseBody 和 @RequestBody 注解的区别
2024-04-16 09:35:00
python 随机生成10位数密码的实现代码
2021-08-01 23:49:02
用python画个敬业福字代码
2022-04-04 18:36:59
python利用pd.cut()和pd.qcut()对数据进行分箱操作
2022-03-26 07:57:45
jupyter-lab设置自启动及远程连接开发环境
2023-08-29 09:11:39
Google Chrome的hack写法以及CSS的支持程度
2008-09-04 12:28:00
MySQL事务控制流与ACID特性
2024-01-24 23:03:50
从一道js笔试题到==运算符的简析
2010-05-10 20:28:00
js表格拖选动态效果COOL而实用
2007-08-05 12:07:00
如何从0开始用node写一个自己的命令行程序
2024-05-03 15:56:26