Python批量对目录下文件重命名
2016-12-29 13:41
537 查看
经常会遇到下载的文件或电子书,名字中间都包含了一些网址信息,实际使用中由于名字太长不方便,下面的脚本使用正则表达式来对目录下的所有文件重命名:
例如:
修改前:[大家网]Mac OS X for Unix Geeks[www.TopSage.com].mobi
修改后:Mac OS X for Unix Geeks.mobi
python代码如下:
import os
import re
def rename_dir(dir,regex,f):
if not os.path.isdir(dir) or not os.path.exists(dir) :
print("The input is not one directory or not exist.")
for root,subdirs,files in os.walk(dir):
for name in files:
oldname = name
newname = re.sub(regex,f,name)
print("Before : " + os.path.join(root,oldname))
print("After : " + os.path.join(root,newname))
if not name == newname and not os.path.exists(os.path.join(root,newname)):
os.rename(os.path.join(root,oldname),os.path.join(root,newname))
for dir in subdirs:
rename_dir(os.path.join(root,dir))
rename_dir("C:\\Python31\\test","\[.*\](.*)\[www.TopSage.com\](.*)",lambda m:m.group(1)+m.group(2))
use warnings;
use File::Find;
my $regex = "\\[.*\\](.*)\\[www.TopSage.com\\](.*)";
# $replace doesn't work
my $replace = "\$1\$2";
sub wanted {
my $name = $File::Find::name;
if( -f $name){
my $newname =$name;
$newname =~ s/$regex/$1$2/;
print "Before: $File::Find::name\n";
print "After : $newname\n";
if( !-e $newname) {
rename($name, $newname);
}
}
}
sub rename_dir{
my ($dir,) = @_;
if (!-d $dir || !-e $dir){
print"The input is not directory or not exist.";
}
find(\&wanted, $dir);
}
&rename_dir("c:\\perl\\test");
例如:
修改前:[大家网]Mac OS X for Unix Geeks[www.TopSage.com].mobi
修改后:Mac OS X for Unix Geeks.mobi
python代码如下:
import os
import re
def rename_dir(dir,regex,f):
if not os.path.isdir(dir) or not os.path.exists(dir) :
print("The input is not one directory or not exist.")
for root,subdirs,files in os.walk(dir):
for name in files:
oldname = name
newname = re.sub(regex,f,name)
print("Before : " + os.path.join(root,oldname))
print("After : " + os.path.join(root,newname))
if not name == newname and not os.path.exists(os.path.join(root,newname)):
os.rename(os.path.join(root,oldname),os.path.join(root,newname))
for dir in subdirs:
rename_dir(os.path.join(root,dir))
rename_dir("C:\\Python31\\test","\[.*\](.*)\[www.TopSage.com\](.*)",lambda m:m.group(1)+m.group(2))
用perl写了下,感觉代码也没有少写多少
use strict;use warnings;
use File::Find;
my $regex = "\\[.*\\](.*)\\[www.TopSage.com\\](.*)";
# $replace doesn't work
my $replace = "\$1\$2";
sub wanted {
my $name = $File::Find::name;
if( -f $name){
my $newname =$name;
$newname =~ s/$regex/$1$2/;
print "Before: $File::Find::name\n";
print "After : $newname\n";
if( !-e $newname) {
rename($name, $newname);
}
}
}
sub rename_dir{
my ($dir,) = @_;
if (!-d $dir || !-e $dir){
print"The input is not directory or not exist.";
}
find(\&wanted, $dir);
}
&rename_dir("c:\\perl\\test");
相关文章推荐
- python 2.7 : 错误处理、调试
- python实例手册
- python 实现递归删除文件和文件夹
- python之基础篇(五)——数据类型
- numpy的 Broadcasting
- pycharm把.py文件识别成.text文件
- Python 错误:IndentationError: expected an indented block
- Python3:好玩游戏的物品清单 和 列表到字典的函数
- 理解Python闭包与延迟绑定
- tornado 入门
- Python编程的中文问题
- caffe+python+mnist从图片训练到测试单张图片
- caffe+python+mnist从图片训练到测试单张图片
- python学习笔记(三)
- Python3 基础:模块
- 操作系统课程设计-线程和进程的同步与互斥
- python2与python3区别汇总
- Cassandra3.9版本基础入门3-插入和查询
- 深度解析minist例子(lasagne)
- Python爬虫爬取动态页面思路+实例(二)