- 论坛徽章:
- 0
|
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.mp3 和 mahadeva.MP3
是同一个文件名。例如,在 Windows 和 Mac OS 下,normcase 将把整个文件名转换
为小写字母;而在 UNIX 兼容的系统下,它将返回未作修改的文件名。
这一段python程序的目的就是获取特定目录下,特定后缀名的文件。
file:///home/wangyao/Desktop/Study/Python/diveintopython-htmlflat-5.4_zh-ch/html/images/callouts/3.png
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/12592/showart_142809.html |
|