本文实例讲述了python实现去除下载电影和电视剧文件名中的多余字符的方法,是一个非常实用的技巧,分享给大家供大家参考。具体如下:
有时候我们讨厌下载电影和电视剧文件名中的多余字符(如网址和广告字样),搞得文件名好长!不便于查看,这时候就可以使用下面的Python代码,自行修改即可.
具体实现代码如下:
#!\usr\bin\env python # -*- coding: utf-8 -*- # Author: 吴徐平 # FileName: RefineFileName.py # Function: # 下载的电影电视文件名太长, # 常常含有多余的字符,如'中英双字幕', # 可以使用本Python代码去掉 # Using python 2.7.X,win xp sp3 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) #print (oldfn.encode('ascii','ignore')+' --> '+newfn.encode('ascii','ignore')) #正则表达式替换,最多一次,忽略大小写 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):#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; # 循环#硬盘的电影电视剧所有目录下的文件 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程序设计能有所帮助。

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment