我做了一个过滤功能来过滤掉文件名列表中的文件类型。
>>> l1
['180px-Cricketball.png', 'AgentVinod_450.jpg', 'Cricketball.bmp', 'Django-1.4', 'Django-1.4.tar.gz', 'Firefox Setup 11.0.exe', 'I-Will-Do-The-Talking-Tonight-(Muskurahat.Com).mp3', 'kahaani-.jpg', 'Never gonna leave this bed.mp3', 'Piya-Tu-Kaahe-Rootha-Re-(Muskurahat.Com).mp3', 'pygame-1.9.1release', 'pygame-1.9.1release.zip', 'pygame-1.9.2a0.win32-py2.7.msi', 'python-2.7.2.msi', 'python-3.1.2.msi', 'Resume.doc', 'selenium-2.20.0', 'selenium-2.20.0.tar.gz', 'sqlite-shell-win32-x86-3071100.zip', 'wxdesign_220a.exe', 'YTDSetup.exe']
>>> def myfilt(subject):
if re.search('.jpg',subject):
return True
>>> filter(myfilt,l1)
['AgentVinod_450.jpg', 'kahaani-.jpg']
这工作正常。
现在假设我想让它更灵活。我想将文件类型传递给函数。所以我重写了函数
>>> def myfilt(subject,filetype):
if re.search(filetype,subject):
return True
现在如何通过过滤器函数传递文件类型?
我试过:
>>> filter(myfilt(l1,filetype),l1)
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
filter(myfilt(l1,filetype),l1)
File "<pyshell#28>", line 2, in myfilt
if re.search(filetype,subject):
File "C:\Python27\lib\re.py", line 142, in search
return _compile(pattern, flags).search(string)
TypeError: expected string or buffer
没有任何工作。有任何想法吗?