• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python和perl实现批量对目录下电子书文件重命名的代码分享

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

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了shell perl python,perl和python,python与perl的区别,python如何实现perl,perl/php/python等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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

修改前:[]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))
</div>

用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");
</div>

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");
</div>

</div>

您可能想查找下面的文章:

  • Python和Perl绘制中国北京跑步地图的方法
  • Python和perl实现批量对目录下电子书文件重命名的代码分享
  • Python和perl实现批量对目录下电子书文件重命名的代码分享
  • Python Mysql数据库操作 Perl操作Mysql数据库

相关文章

  • python实现下载指定网址所有图片的方法
  • Python的高级Git库 Gittle
  • Python中多线程thread与threading的实现方法
  • 在Django中管理Users和Permissions以及Groups的方法
  • Python的requests网络编程包使用教程
  • Python用GET方法上传文件
  • Python实现将xml导入至excel
  • python 正则式 概述及常用字符
  • 举例讲解Python中is和id的用法
  • 举例讲解Python程序与系统shell交互的方式

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python制作爬虫并将抓取结果保存到excel中
    • python中列表元素连接方法join用法实例
    • Python读取环境变量的方法和自定义类分享
    • python中while循环语句用法简单实例
    • python根据文件大小打log日志
    • python制作花瓣网美女图片爬虫
    • Python正则表达式介绍
    • python发布模块的步骤分享
    • 用Python写飞机大战游戏之pygame入门(4):获取鼠标的位置及运动
    • python制作最美应用的爬虫

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有