Python 文件重命名工具代码
发布时间:2022-09-25 05:15:32
标签:Python,文件重命名
#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)


猜你喜欢
- 问题你想使用一个简单的REST接口通过网络远程控制或访问你的应用程序,但是你又不想自己去安装一个完整的web框架。解决方案构建一个REST风
- 一、什么是框架框架的本质就是一个socket服务,可以完成不同主机之间的通信。它是一个半成品的项目,其中可能已经封装好了基本的功能,比如路由
- 由于下载这个clsExport2Excel 代码时,已经找不到代码出处感谢上传这个类的朋友,为大家带来了方便我只对其中的属性传递做了一些调整
- 表单是让用户与我们的网页应用程序交互的基本元素。Flask 本身并不会帮助我们处理表单,但是 Flask-WTF 扩展让我们在我们的 Fla
- python random库简单使用demo当我们需要生成随机数或者从一个序列中随机选择元素时,可以使用 Python 内置的 random
- 如果可以减少过多的外部隔离的API和简化部署的细节 这会是非常好的。在以前的文章中,我解释了"一些使用反向代理的好处&
- 目录快速使用模式总结今天学个简单点的😀,termtables处理表格形式数据的输出。适用于随时随地的输出一些状态或统计数据,便于观察和调试。
- 本文实例讲述了Python实现对象转换为xml的方法。分享给大家供大家参考,具体如下:# -*- coding:UTF-8 -*-'
- 本文实例讲述了sql server实现在多个数据库间快速查询某个表信息的方法。分享给大家供大家参考,具体如下:最近出来实习,所在公司的服务器
- 去年曾总结了《IE对CSS样式表的限制和解决方案》中限制的第4条写道“一个CSS文件的不能超过288kb?”,这是一个疑问句,当时没有重现出
- 字体的处理在网页设计中无论怎么强调也不为过, 毕竟网页使用来传递信息的, 而最经典最直接的信息传递方式就是文字,&nbs
- 引言承接上篇 parseHTML 函数源码解析拿到返回值后的处理接下来我们将会讲解当 textEnd === 0 解析器遇到结束标签,par
- 一 实战1 Django_lab\urls.py# -*- coding: utf-8 -*-from django.c
- 本文实例讲述了python单向链表的基本实现与使用方法。分享给大家供大家参考,具体如下:# -*- coding:utf-8 -*-#! p
- 列表(list)和元组(tuple)的一些基础list和tuple都是一个可以放置任意数据类型的有序集合,都是既可以存放数字、字符串、对象等
- 相关文章Pytest框架之fixture详解(一)Pytest框架之fixture详解(二)Pytest框架之fixture详解(三)本文关
- 本文实例讲述了js实现向右横向滑出的二级菜单效果。分享给大家供大家参考。具体如下:这是一个网页上的横向滑出二级菜单,菜单是竖向排列的,但二级
- mysql5.7设置远程访问不是和网上说的一样建个用户赋个权限就可以访问的。比如下边这个就是建用户赋权限,可能在之前的版本可以,但是我在我的
- 网站上传图片后生成缩略图应该是非常常用的功能了,通常来讲为了网站显示美观,缩略图会是同样尺寸,比如最近笔者做的一个站点,缩略图规格要求都是1
- function clearCookie(){ var keys=document.cookie.match(/[^ =;]+(?=\=)/