国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - Python和perl實(shí)現(xiàn)批量對目錄下電子書文件重命名的代碼分享

Python和perl實(shí)現(xiàn)批量對目錄下電子書文件重命名的代碼分享

2020-05-14 09:31腳本之家 Python

這篇文章主要介紹了Python和perl實(shí)現(xiàn)批量對目錄下電子書文件重命名的代碼分享,本文同時(shí)給出了Python和perl兩種語言的實(shí)現(xiàn)代碼,需要的朋友可以參考下

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

修改前:[服務(wù)器之家]Mac OS X for Unix Geeks[www.jfrwli.cn].mobi
修改后:Mac OS X for Unix Geeks.mobi

python代碼如下:

復(fù)制代碼 代碼如下:

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.jfrwli.cn\](.*)",lambda m:m.group(1)+m.group(2))

 

用perl寫了下,感覺代碼也沒有少寫多少

 

復(fù)制代碼 代碼如下:

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

 

my $regex = "\\[.*\\](.*)\\[www.jfrwli.cn\\](.*)";
# $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 實(shí)現(xiàn)2

 

復(fù)制代碼 代碼如下:

use strict;
use warnings;

 

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

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 一区二区在线视频 | 精品国产欧美一区二区三区成人 | 日韩午夜免费 | 国产精品成人在线 | 日本免费高清视频 | 99热视 | 四虎免费在线播放 | 日韩精品视频在线 | 久久66| av电影在线播放 | 99手机在线视频 | 色婷婷精品国产一区二区三区 | 亚洲久草视频 | 国产一区精品在线 | 亚洲一区 日韩精品 中文字幕 | 亚洲综合视频 | 夜夜爽av福利精品导航 | av成人免费在线观看 | 久久久久久国产免费 | 自拍偷拍精品 | 欧美大片高清在线观看平台 | 色图综合 | 免费成人av网址 | 在线国产视频 | 欧美成人a | 国产精品视频久久久 | 亚洲在线电影 | 午夜爽视频 | 在线激情网 | 国产精品视屏 | 中文字幕高清一区 | 亚洲成人一级片 | 亚洲欧洲精品视频在线观看 | 精品久久久网站 | www.久久久 | 亚洲视频一区二区 | 亚洲 自拍 另类 欧美 丝袜 | 岛国一区| 色婷婷影院 | 91精品免费在线观看 | 国产一区免费 |