Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5705030
  • 博文数量: 675
  • 博客积分: 20301
  • 博客等级: 上将
  • 技术积分: 7671
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-31 16:15
文章分类

全部博文(675)

文章存档

2012年(1)

2011年(20)

2010年(14)

2009年(63)

2008年(118)

2007年(141)

2006年(318)

分类: Python/Ruby

2006-07-19 19:04:09

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)]
fileList = [os.path.join(directory, f)
for f in fileList
if os.path.splitext(f)[1] in fileExtList]

这里面,os.path.splitext获得的是一个元组。
>>> print os.path.splitext.__doc__
Split the extension from a pathname. Extension is everything from the
last dot to the end. Returns "(root, ext)", either part may be empty.
>>> a=('1','2')
>>> a[1]
'2'
所以,os.path.splitext(f)[1]获得的是后缀名。

其中,os.path.normcase(f)是将后缀名进行转换。os.path.normcase(f) 根据
操作系统的缺省值对大小写进行标准化处理。 normcase 是一个有用的函数,用于对
大小写不敏感操作系统的一个补充。这种操作系统认为 mahadeva.mp3mahadeva.MP3
是同一个文件名。例如,在 Windows 和 Mac OS 下,normcase 将把整个文件名转换
为小写字母;而在 UNIX 兼容的系统下,它将返回未作修改的文件名。


这一段python程序的目的就是获取特定目录下,特定后缀名的文件。
3
阅读(3003) | 评论(4) | 转发(0) |
给主人留下些什么吧!~~