讨厌下载电影和电视剧文件名中的多余字符(如网址和广告字样),,搞得文件名好长,可以使用下面的Python代码,自行修改即可.
-
- import sys
- import os
- import re
-
-
- FileDirectoryList=[\
- u"E:\\电视电影\\都市侠盗.Leverage",\
- u"E:\\电视电影\\犯罪心理.Criminal.Minds",\
- u"E:\\电视电影\\海军罪案调查处.NCIS",\
- u"E:\\电视电影\\警察世家.Blue.Bloods"\
- ]
-
- AddStringList=[\
- u‘Leverage‘,\
- u‘Criminal.Minds‘,\
- u‘NCIS.‘,\
- u‘Blue.Bloods.‘
- ]
-
- MustReplaceStringList=[\
- u‘都市侠盗‘,\
- u‘犯罪心理‘,\
- u‘海军罪案调查处‘,\
- u‘警察世家‘,\
- u‘人人影视‘,\
- u‘SFiles‘,\
- u‘YYeTs‘,\
- ]
-
-
-
- NewString=u‘‘;
-
- def RenameFileName(OldFileName,NewFileName):
- oldpath,oldfn = os.path.split(OldFileName)
- newpath,newfn = os.path.split(NewFileName)
- print(oldpath.encode(‘ascii‘,‘ignore‘))
- os.rename(OldFileName, NewFileName)
-
-
-
- def StringRegexReplace(pattern,repl,string):
- return re.sub(pattern, repl, string, count=1, flags=re.I)
-
-
-
- for FileDirectory in FileDirectoryList:
- FileNamesList=os.listdir(FileDirectory)
-
- for filenamei in FileNamesList:
-
- RefinedFileName=filenamei.replace(u‘ ‘,NewString)
-
- for MustReplaceString in MustReplaceStringList:
- RefinedFileName=RefinedFileName.replace(MustReplaceString,NewString)
-
- RefinedFileName=StringRegexReplace(u‘(?<=[\.\-_])[^.]*rip(?=[\.\-_])‘,‘‘,RefinedFileName)
- RefinedFileName=StringRegexReplace(u‘(?<=[\.\-_])xvi[^.]*(?=[\.\-_])‘,‘‘,RefinedFileName)
-
- RefinedFileName=StringRegexReplace(‘\d{1,4}X\d{1,4}‘,‘‘,RefinedFileName)
-
- RefinedFileName=StringRegexReplace(u‘\[.*\]‘,‘‘,RefinedFileName)
-
- RefinedFileName=StringRegexReplace(u‘www\..*\.((com)|(net)|(cn)|(org))‘,‘‘,RefinedFileName)
-
- RefinedFileName=StringRegexReplace(u‘(?<=[\.\-_])[^.]*字幕组?‘,‘‘,RefinedFileName)
-
- RefinedFileName=StringRegexReplace(u‘(?<=[\.\-_])[^.]*[出作]品‘,‘‘,RefinedFileName)
-
- RefinedFileName=StringRegexReplace(u‘^[._\-]‘,‘‘,RefinedFileName)
-
- RefinedFileName=RefinedFileName.replace(u‘...‘,u‘.‘)
- RefinedFileName=RefinedFileName.replace(u‘..‘,u‘.‘)
-
-
-
- OldFileName=os.path.join(FileDirectory,filenamei)
- NewFileName=os.path.join(FileDirectory,RefinedFileName)
- RenameFileName(OldFileName,NewFileName)
-
-
- def HasAddString(AddString0,FileNameString0):
- if (len(AddString0.strip())<1):
- print(‘Empty AddString,No need to add to : ‘+ FileNameString0.encode(‘ascii‘,‘ignore‘))
- return True
- else:
- AddString=AddString0.strip().lower()
- FileNameString=FileNameString0.strip().lower()
- if(len(FileNameString)<=len(AddString)):
- return False
- else:
- if(FileNameString[0:(len(AddString)-1)]==AddString[0:(len(AddString)-1)]):
- return True
- else:
- return False
-
-
-
- DirCounter=0;
- for FileDirectory in FileDirectoryList:
- FileNamesList=os.listdir(FileDirectory)
- AddString =AddStringList[DirCounter]
- DirCounter=DirCounter+1
-
- for filenamei in FileNamesList:
-
- if HasAddString(AddString,filenamei):
- print(filenamei.encode(‘ascii‘,‘ignore‘)+‘ : ALready Added Header String!‘)
- else:
-
- RefinedFileName=AddString+filenamei
-
- OldFileName=os.path.join(FileDirectory,filenamei)
- NewFileName=os.path.join(FileDirectory,RefinedFileName)
- RenameFileName(OldFileName,NewFileName)
- print(filenamei.encode(‘ascii‘,‘ignore‘)+‘ : Add Header String, OK!‘)
-
-
-
-
对文件名的修改有特殊要求的,可以将StringRegexReplace函数用上.
下面的电视剧文件名看起来舒服多了:

看看文件名是不是清爽许多.....
去除下载电影和电视剧文件名中的多余字符[python实现]
原文:http://www.cnblogs.com/rrxc/p/3974146.html