Dive Into Python 学习记录3-对获取某文件夹下MP3文件信息的代码构成分析
来源:互联网 发布:fdm和idm 知乎 编辑:程序博客网 时间:2024/05/01 04:39
代码为:
"""Framework for getting filetype-specific metadata.Instantiate appropriate class with filename. Returned object acts like adictionary, with key-value pairs for each piece of metadata. import fileinfo info = fileinfo.MP3FileInfo("/music/ap/mahadeva.mp3") print "\\n".join(["%s=%s" % (k, v) for k, v in info.items()])Or use listDirectory function to get info on all files in a directory. for info in fileinfo.listDirectory("/music/ap/", [".mp3"]): ...Framework can be extended by adding classes for particular file types, e.g.HTMLFileInfo, MPGFileInfo, DOCFileInfo. Each class is completely responsible forparsing its files appropriately; see MP3FileInfo for example.This program is part of "Dive Into Python", a free Python book forexperienced programmers. Visit http://diveintopython.org/ for thelatest version."""__author__ = "Mark Pilgrim (mark@diveintopython.org)"__version__ = "$Revision: 1.3 $"__date__ = "$Date: 2004/05/05 21:57:19 $"__copyright__ = "Copyright (c) 2001 Mark Pilgrim"__license__ = "Python"import osimport sysfrom UserDict import UserDictdef stripnulls(data): "strip whitespace and nulls" return data.replace("\00", " ").strip() # .strip()移除字符串首尾在括号内指定参数的字符,默认为空格class FileInfo(UserDict): "store file metadata" def __init__(self, filename=None): UserDict.__init__(self) self["name"] = filename class MP3FileInfo(FileInfo): "store ID3v1.0 MP3 tags" tagDataMap = {"title" : ( 3, 33, stripnulls), "artist" : ( 33, 63, stripnulls), "album" : ( 63, 93, stripnulls), "year" : ( 93, 97, stripnulls), "comment" : ( 97, 126, stripnulls), "genre" : (127, 128, ord)} def __parse(self, filename): "parse ID3v1.0 tags from MP3 file" self.clear() try: fsock = open(filename, "rb", 0) try: fsock.seek(-128, 2) tagdata = fsock.read(128) finally: fsock.close() if tagdata[:3] == 'TAG': for tag, (start, end, parseFunc) in self.tagDataMap.items(): self[tag] = parseFunc(tagdata[start:end]) except IOError: pass def __setitem__(self, key, item): if key == "name" and item: self.__parse(item) FileInfo.__setitem__(self, key, item)def listDirectory(directory, fileExtList): "get list of file info objects for files of particular extensions" fileList = [os.path.normcase(f) for f in os.listdir(directory)] #os.path.normcase(f)表示转换f的大小写和斜杠;os.listdir(directory)列出 dir 里面 #的所有文件和目录,但不包括子目录中的内容 fileList = [os.path.join(directory, f) for f in fileList \ #path.join(directory, f)把目录和文件名合成一个路径;os.path.splitext(f)[1]分割路径, #返回路径名和文件扩展名的元组 if os.path.splitext(f)[1] in fileExtList] def getFileInfoClass(filename, module=sys.modules[FileInfo.__module__]): "get file info class from filename extension" subclass = "%sFileInfo" % os.path.splitext(filename)[1].upper()[1:] #进行大写转换 return hasattr(module, subclass) and getattr(module, subclass) or FileInfo #hasattr(os, 'system')查看os对象是否有system属性,有返回True,否则False #getattr(os, 'system') 得到os对象的system属性 return [getFileInfoClass(f)(f) for f in fileList]if __name__ == "__main__": for info in listDirectory("E:\Kugou\Listen", [".mp3"]): print "\n".join(["%s=%s" % (k, v) for k, v in info.items()]) print
输出为:
album=comment=name=E:\Kugou\Listen\makiyo - wake up.mp3title=artist=year=genre=12album=needpop.comcomment=needpop.comname=E:\Kugou\Listen\sara - 我的心好冷.mp3title=我的心好冷(完整版)artist=SARA needpop.comyear=2010genre=255album=⊙完美工作室⊙comment=name=E:\Kugou\Listen\三轮兄弟 斗地主 川普.mp3title=斗地主(川普)artist=三轮兄弟year=genre=0album=绮楼ぁ听风雨收藏Qq:1505388177comment=name=E:\Kugou\Listen\付辛博 - 我,一个人.mp3title=artist=year=genre=255name=E:\Kugou\Listen\伍佰、china blue - 心如刀割.mp3name=E:\Kugou\Listen\冷漠 - 醉红颜.mp3name=E:\Kugou\Listen\冷漠、云菲菲 - 小三.mp3album=comment=name=E:\Kugou\Listen\凤凰传奇 - 大声唱.mp3title=artist=year=genre=12
解释与拓展:
>>> num'i like pYthon'>>> ' spacious '.strip()'spacious'>>> 'www.example.com'.strip('comz.')'www.example'>>> 'www.example.com'.strip('comwz.')'example'
>>> num.strip('on')'i like pYth'>>> num.strip('th')'i like pYthon'>>> 'www.example.com'.strip('comw.')'example'>>> 'www.example.com'.strip('comwe.')'xampl'>>> 'www.example.com'.strip('comwx.')'example'>>> 'www.example.com'.strip('lecomw.')'xamp'strip ()只是去除字符串首尾的第一个开始匹配的指定字符,第一次去除后若首尾还有指定字符,则继续去除,直到首尾无指定的参数,对中间的原样保留,
2、hasattr(os, 'system')查看os对象是否有system属性,有返回True,否则False ;getattr(os, 'system') 得到os对象的system属性
- int PyObject_HasAttr(PyObject *o,PyObject *attr_name)
Returns 1 if o has the attributeattr_name, and0 otherwise. Thisis equivalent to the Python expressionhasattr(o,attr_name). This functionalways succeeds.
- int PyObject_HasAttrString(PyObject *o, const char *attr_name)
Returns 1 if o has the attributeattr_name, and0 otherwise. Thisis equivalent to the Python expressionhasattr(o,attr_name). This functionalways succeeds.
- PyObject*PyObject_GetAttr(PyObject *o,PyObject *attr_name)
- Return value: New reference.
Retrieve an attribute named attr_name from object o. Returns the attributevalue on success, orNULL on failure. This is the equivalent of the Pythonexpressiono.attr_name.
- PyObject*PyObject_GetAttrString(PyObject *o, const char *attr_name)
- Return value: New reference.
Retrieve an attribute named attr_name from object o. Returns the attributevalue on success, orNULL on failure. This is the equivalent of the Pythonexpressiono.attr_name.
from 模块名 import 模块名from UserDict import UserDict
它与你所熟知的 import module 语法很相似,但是有一个重要的区别:UserDict 被直接导入到局部名字空间去了,所以它可以直接使用,而不需要加上模块名的限定。你可以导入独立的项或使用frommodule import * 来导入所有东西。
比较 import 模块名
>>> import odbchelper >>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}>>> print odbchelper.buildConnectionString(params) #加上模块名使用在被导入模块中定义的函数时,必须包含模块的名字。所以不能只使用 buildConnectionString,而应该使用odbchelper.buildConnectionString。
Python 中的frommodule import * 像Perl 中的usemodule ;Python 中的importmodule 像Perl 中的requiremodule 。Python 中的frommodule import * 像Java 中的importmodule.* ;Python 中的importmodule 像Java 中的importmodule 。
>>> import types>>> types.FunctionType <type 'function'>>>> FunctionType Traceback (innermost last): File "<interactive input>", line 1, in ?NameError: There is no variable named 'FunctionType'>>> from types import FunctionType >>> FunctionType <type 'function'>
什么时候你应该使用 from module import?
- 如果你要经常访问模块的属性和方法,且不想一遍又一遍地敲入模块名,使用 from module import。
- 如果你想要有选择地导入某些属性和方法,而不想要其它的,使用 from module import。
- 如果模块包含的属性和方法与你的某个模块同名,你必须使用 import module 来避免名字冲突。
class FileInfo(UserDict): "store file metadata" def __init__(self, filename=None):
类中的所有东西都要缩近,就像位于函数、if 语句,for 循环,诸如此类的代码。第一条不缩近的东西不属于这个类;
在 Python 中,类的基类只是简单地列在类名后面的小括号里。所以 FileInfo 类是从 UserDict 类 (它是从 UserDict 模块导进来的) 继承来的。UserDict 是一个像字典一样工作的类,它允许你完全子类化字典数据类型,同时增加你自已的行为。{也存在相似的类UserList 和UserString ,它们允许你子类化列表和字符串。)
class FileInfo(UserDict): "store file metadata" def __init__(self, filename=None): UserDict.__init__(self) self["name"] = filename一些伪面向对象语言,像 Powerbuilder 有一种“扩展”构造函数和其它事件的概念,即父类的方法在子类的方法执行前被自动调用。Python 不是这样,你必须显示地调用在父类中的合适方法。这个类像字典一样工作,那么这里就是第一个印象。我们将参数 filename 赋值给对象name 关键字,作为它的值。注意 __init__ 方法从不返回一个值。
>>> class C:def __init__(self,val):self.val = valdef f(self): print "hello, my value is:", self.val>>> a = C(27)>>> b = C(42)>>> a.f()hello, my value is: 27>>> b.f()hello, my value is: 42>>> C.f(a)hello, my value is: 27>>> C.f(b)hello, my value is: 42
从上例可以看出既可以通过python自动补充参数进行实例引用,也可以通过对象将值传递;
当定义你自已的类方法时,你必须 明确将 self 作为每个方法的第一个参数列出,包括 __init__。当从你的类中调用一个父类的一个方法时,你必须包括self 参数。但当你从类的外部调用你的类方法时,你不必对self 参数指定任何值;你完全将其忽略,而Python 会自动地替你增加实例的引用。
http://www.freenetpages.co.uk/hp/alan.gauld/tutclass.htm
- Dive Into Python 学习记录3-对获取某文件夹下MP3文件信息的代码构成分析
- dive into python (3)
- Dive Into Python学习日志
- 获取MP3文件信息的代码
- Dive Into Python 学习记录3-getattr 介绍/过滤列表/and or/lambda 函数
- Dive Into Python 学习记录2-自省/info 函数 /str / type /callable 函数
- Dive Into Python
- Dive into Python 点滴
- dive into python (2)
- dive into python
- python_批量获取指定文件夹下的所有文件的厂商信息
- 用于获取Mp3文件信息的类
- android获取MP3文件的歌曲信息
- Dive Into Python 学习记录1-函数/模块导入/字典/列表/元组/字符串分割、连接、格式化/映射list/
- Dive into python第七章正则表达式学习笔记
- Dive into Python第九章xml学习笔记
- Python获取文件夹下的文件和子文件夹
- dive into Python (1)
- 不吃早饭后期易得很多的病?胃炎,胆结石。。。
- 缺钙吃这些蔬菜能补钙
- Esri美国用户大会Q&A 之 ArcGIS Server未来发展
- sql操作数据库修改字段中的某些字串
- ArcObjects中的IGeometry转成Json
- Dive Into Python 学习记录3-对获取某文件夹下MP3文件信息的代码构成分析
- Spring Security oAuth学习之Hello World
- Redis复制流程概述
- libevent源码深度剖析
- Re-installation failed due to different application signatures.
- 每日一读---补充正能量
- 大数据与信息社会的意义结构【转自阿里巴巴研究院】
- Redis内存使用优化与存储
- 如何让ipad 启动时默认横屏