Python和perl實(shí)現(xiàn)批量對目錄下電子書文件重命名的代碼分享
經(jīng)常會遇到下載的文件或電子書,名字中間都包含了一些網(wǎng)址信息,實(shí)際使用中由于名字太長不方便,下面的腳本使用正則表達(dá)式來對目錄下的所有文件重命名:
例如:
修改前:[腳本之家]Mac OS X for Unix Geeks[www.dbjr.com.cn].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.dbjr.com.cn\](.*)",lambda m:m.group(1)+m.group(2))
用perl寫了下,感覺代碼也沒有少寫多少
use strict;
use warnings;
use File::Find;
my $regex = "\\[.*\\](.*)\\[www.dbjr.com.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
use strict;
use warnings;
my $regex = "\\[.*\\](.*)\\[www.dbjr.com.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");
相關(guān)文章
Python 按字典dict的鍵排序,并取出相應(yīng)的鍵值放于list中的實(shí)例
今天小編就為大家分享一篇Python 按字典dict的鍵排序,并取出相應(yīng)的鍵值放于list中的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02簡單介紹Python中的filter和lambda函數(shù)的使用
這篇文章主要簡單介紹了Python中的filter和lambda函數(shù)的使用,是Python學(xué)習(xí)中的基礎(chǔ),同時(shí)lambda匿名函數(shù)的使用也是經(jīng)常被用來對比各種編程語的重要特性,言需要的朋友可以參考下2015-04-04對python以16進(jìn)制打印字節(jié)數(shù)組的方法詳解
今天小編就為大家分享一篇對python以16進(jìn)制打印字節(jié)數(shù)組的方法詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01python網(wǎng)絡(luò)編程之?dāng)?shù)據(jù)傳輸U(kuò)DP實(shí)例分析
這篇文章主要介紹了python網(wǎng)絡(luò)編程之?dāng)?shù)據(jù)傳輸U(kuò)DP實(shí)現(xiàn)方法,實(shí)例分析了Python基于UDP協(xié)議的數(shù)據(jù)傳輸實(shí)現(xiàn)方法,需要的朋友可以參考下2015-05-05Python hashlib庫數(shù)據(jù)安全加密必備指南
這篇文章主要為大家介紹了Python hashlib庫數(shù)據(jù)安全加密的使用實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01淺談Django自定義模板標(biāo)簽template_tags的用處
這篇文章主要介紹了淺談Django自定義模板標(biāo)簽template_tags的用處,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12對Python的交互模式和直接運(yùn)行.py文件的區(qū)別詳解
今天小編就為大家分享一篇對Python的交互模式和直接運(yùn)行.py文件的區(qū)別詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06基于Python實(shí)現(xiàn)模擬三體運(yùn)動(dòng)的示例代碼
此前所做的一切三體和太陽系的動(dòng)畫,都是基于牛頓力學(xué)的,而且直接對微分進(jìn)行差分化,從而精度非常感人,用不了幾年就得撞一起去。所以本文來用Python重新模擬一下三體運(yùn)動(dòng),感興趣的可以了解一下2023-03-03