Python implements methods to remove unwanted characters from the file name of a movie and TV show _python

Source: Internet
Author: User

This article describes the Python implementation of the removal of the download movie and TV series in the file name of the redundant characters, is a very practical skill, share for everyone to reference. Specifically as follows:

Sometimes we hate downloading the extra characters (such as URLs and advertisements) in the file name of the movie and TV series, so the file name is very long! Not easy to view, this time you can use the following Python code, modify it yourself.

The specific implementation code is as follows:

#!\usr\bin\env python #-*-coding:utf-8-*-# Author: Wu Xuping # FileName:RefineFileName.py # Function: # Download the movie TV file name too Long, # often contain extra characters, such as ' Chinese-English subtitles ', # can be removed using this Python code # using Python 2.7.x,win XP SP3 import sys import OS import re # folder Directory listing filedirectorylist=[\ u "e:\\ TV movie \ City Grand theft. Leverage ", \ U" e:\\ TV film \ \ Crime Psychology. Criminal.minds ", U" e:\\ TV film/Navy crime investigation office. NCIS ", u" e:\\ TV movie/police family. Blue.bloods "\] #文件名开头处是否需要添加的字符串 #不需要添加字符时, leave blank (or blank) character to indicate addstringlist=[\ U ' leverage ', \ U ' criminal. Minds ', \ U ' NCIS. ', \ U ' Blue. 
Bloods. ' # The characters that must be removed in the file name mustreplacestringlist=[\ U ' Metropolis Grand Theft ', U ' criminal psychology ', \ U ' Naval Crime Investigation Office ', U ' Police family ', \ U ' all film and television ', \ u ' sfiles ', \ U ' yye 
 
Ts ', \] # Remove all unnecessary characters newstring=u ';  # Custom Rename file function 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) #print (Oldfn.encode (' ASCII ', ' ignore ') + '--> ' +newfn.encode (' ASCII ', ' ignore ') #正则表达式替换, at most once, ignoring case def stringregexreplace (pattern,repl,string): Return re.sub (Pattern, REPL, str ING, count=1, flags=re. I) # cycle # HDD's movie Show all directories under the file for FileDirectory in Filedirectorylist:filenameslist=os.listdir (filedirectory) # Loop rename file for Filenamei in Filenameslist: # First remove the space characters in the filename refinedfilename=filenamei.replace (U ', Newst Ring) # The list of file names that must be removed for the mustreplacestring in MUSTREPLACESTRINGLIST:REFINEDFILENAME=REFINEDFILENAME.REPL Ace (mustreplacestring,newstring) #去掉录制信息 refinedfilename=stringregexreplace (U ' (? <=[\.\-_]) [^.] *rip (? =[\.\-_]) ', ', Refinedfilename refinedfilename=stringregexreplace (U ' (? <=[\.\-_]) xvi[^.] * (? =[\.\-_]) ', ', Refinedfilename #去掉分辨率 refinedfilename=stringregexreplace (' \d{1,4}x\d{1,4} ', ', refinedfilename All content in [*] #去掉 [*] refinedfilename=stringregexreplace (U ' \[.*\] ', ', Refinedfilename ') #去掉网址 refinedfilename =stringregexreplace (U ' www\. *\. ((COM)| (NET) | (CN) | (org)) ', ', Refinedfilename ' #去掉字幕 (group) Refinedfilename=stringregexreplace (U ' (? <=[\.\-_]) [^.] * Subtitle Group ",", Refinedfilename) #去掉出品和作品字样 refinedfilename=stringregexreplace (U ' (? <=[\.\-_]) [^.]  
    *[out as] goods ', ', Refinedfilename ' #去掉开头的点. Underline _ Connector-refinedfilename=stringregexreplace (U ' ^[._\-] ', ', refinedfilename) 
    # More than two points are often present, replaced directly with a point refinedfilename=refinedfilename.replace (U ' ... ', U '. ') 
     
    Refinedfilename=refinedfilename.replace (U ' ... ', U '. ') # You can rename the Oldfilename=os.path.join (Filedirectory,filenamei) newfilename=os.path.join (Filedirectory,refinedfilena Me) Renamefilename (oldfilename,newfilename) ### #下面的代码在文件头添加字符串 #判断是否已经添加了字符串 def hasaddstring (Addstring0,filena MESTRING0): if (Len (Addstring0.strip ()) <1): #AddString0为空字符不需要再添加任何字符了 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; # loop # hard drive movie TV show files in all directories for filedirectory in Filedirectorylist:filenameslist=os.listdir (filedirectory) addstring =Ad Dstringlist[dircounter] Dircounter=dircounter+1 # Loop renames the file for Filenamei in Filenameslist: #已经加过的文 The name no longer adds characters if hasaddstring (Addstring,filenamei): Print (Filenamei.encode (' ASCII ', ' ignore ') + ': Already Added 
    Der string! ') Else: # The first connection string Refinedfilename=addstring+filenamei # can be renamed Oldfilename=os.path.join (Filedir Ectory,filenamei) Newfilename=os.path.join (filedirectory,refinedfilename) renamefilename (OldFileName,NewFil 

 ename) Print (Filenamei.encode (' ASCII ', ' ignore ') + ': Add Header String, ok! ')

There are special requirements for modifying file names, and you can use the Stringregexreplace function.

After running this example, you can see that the following TV series file name looks much more comfortable:

I hope that the examples described in this article will help you with your Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.