麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Python > 正文

Python和perl實現批量對目錄下電子書文件重命名的代碼分享

2020-02-23 06:11:55
字體:
來源:轉載
供稿:網友

經常會遇到下載的文件或電子書,名字中間都包含了一些網址信息,實際使用中由于名字太長不方便,下面的腳本使用正則表達式來對目錄下的所有文件重命名:
例如:

修改前:[武林站長站]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 = "http://[.*//](.*)//[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 = "http://[.*//](.*)//[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;}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日韩毛片一区二区三区 | 精品国产91久久久久久浪潮蜜月 | 91不雅视频| 日韩大片在线永久观看视频网站免费 | 国产免费久久久久 | 欧美一区二区黄色 | 天堂二区 | www.91pron| 91精品国产刺激国语对白 | 激情视频在线播放 | 在线播放免费人成毛片乱码 | 日日狠狠久久偷偷四色综合免费 | 黄色毛片一级视频 | 日本在线免费观看 | 亚洲精品在线观看网站 | 九九热精品在线 | 欧美在线观看视频一区二区 | 日本中文字幕网址 | 国产精品区一区二区三区 | 欧美a黄| 国产精品剧情一区二区三区 | 成人在线不卡 | 亚洲视频在线一区二区 | 国产毛片网站 | 免费观看一区二区三区视频 | 国产伦精品一区二区三区在线 | 午夜国产精品成人 | 国产精品剧情一区二区在线观看 | 精品国产96亚洲一区二区三区 | 久久久久一区二区三区四区五区 | 久久综合九色 | 国产日韩一区二区三区在线观看 | 欧美一级免费看 | av影院在线播放 | 国产伦精品一区二区三区在线 | 亚洲第一页中文字幕 | 国产精品性夜天天视频 | 久久电影一区二区 | 欧美2区| 一级毛片免费高清 | 精品久久久久久久久久久久久久 |