Python和perl实现批量对目录下电子书文件重命名的代码分享

yipeiwu_com6年前Python基础

经常会遇到下载的文件或电子书,名字中间都包含了一些网址信息,实际使用中由于名字太长不方便,下面的脚本使用正则表达式来对目录下的所有文件重命名:
例如:

修改前:[【听图阁-专注于Python设计】]Mac OS X for Unix Geeks[www.jb51.net].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.jb51.net\](.*)",lambda m:m.group(1)+m.group(2))

用perl写了下,感觉代码也没有少写多少

复制代码 代码如下:

use strict;
use warnings;
use File::Find;

my $regex = "\\[.*\\](.*)\\[www.jb51.net\\](.*)";
# $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");

perl 实现2

复制代码 代码如下:

use strict;
use warnings;

my $regex = "\\[.*\\](.*)\\[www.jb51.net\\](.*)";
# $replace doesn't work
my $replace = "\$1\$2";

sub rename_dir{
    my $dir = shift;
    if (!-d $dir || !-e $dir){
      print"The input is not directory or not exist.";
    }
    opendir(DIR, $dir) || die "Cannot opendir $dir.";
    foreach (readdir(DIR)) {
      if ($_ eq '.' || $_ eq '..') {next;}
      my $name = $dir.'/'.$_;
      if(-d $name){
        rename_dir($name);        
        next;
        }
      my $newname =$_;
      $newname =~ s/$regex/$1$2/;
      $newname = $dir.'/'.$newname;
      print "Before : $name\n";
      print "After  : $newname\n";
      rename($name,$newname);
    }
    #closedir(DIR);
}
&rename_dir("c:\\perl\\test");

相关文章

Python 高级专用类方法的实例详解

Python 高级专用类方法的实例详解 除了 __getitem__ 和 __setitem__ 之外 Python 还有更多的专用函数。某些可以让你模拟出你甚至可能不知道的功能。下面的...

网红编程语言Python将纳入高考你怎么看?

网红编程语言Python将纳入高考你怎么看?

近日,2018年最具就业前景的7大编程语言排行榜出炉了。这次的编程语言排行榜是由CodingDojo(编码道场)发布。在此次的最有“钱”途的编程语言榜单上,Java排名第一,网红编程语言...

Python排序搜索基本算法之堆排序实例详解

Python排序搜索基本算法之堆排序实例详解

本文实例讲述了Python排序搜索基本算法之堆排序。分享给大家供大家参考,具体如下: 堆是一种完全二叉树,堆排序是一种树形选择排序,利用了大顶堆堆顶元素最大的特点,不断取出最大元素,并调...

pandas 数据归一化以及行删除例程的方法

如下所示: #coding:utf8 import pandas as pd import numpy as np from pandas import Series,DataFra...

windows下安装python的C扩展编译环境(解决Unable to find vcvarsall.bat)

windows下安装python的C扩展编译环境(解决Unable to find vcvarsall.bat)

N久没有开始写博客了,总觉得要随便记点东西,岁月蹉跎,曾经搞得一些东西、技术、工具,说丢也就丢了,点点滴滴还是要记录一下吧。。。    在windows下使用pip安装一些python的...