源代码参考_Python_cookbook
代码如下:
- import os, itertools
-
def anyTrue(predicate,sequence):
-
return True in itertools.imap(predicate,sequence)
-
-
def endsWith(s,*endings):
-
return anyTrue(s.endswith,endings)
-
'''
-
s.endswith 是一个方法,判断后缀名是否是在指定的endings里,如果是返回True
-
anyTrue(s.endswith,endings) 将这个方法传递给anyTrue,
-
itertools.imap(predicate,sequence) 就相当于filename.endswith(enddings)
-
返回结果为True就执行print filename
-
'''
-
-
for filename in os.listdir('.'):
-
if endsWith(filename,'.jpg','.jpeg','.gif'):
- print filename
- endswith判断文件后缀名是否在列表当中
- str.endswith(suffix[, start[, end]])
-
Return True if the string ends with the specified suffix, otherwise return False. suffix can also be a tuple of suffixes to look for. With optional start, test beginning at that position. With optional end, stop comparing at that position.
-
-
Example:
-
-
#!/usr/bin/python
-
-
str = "this is string example....wow!!!";
-
-
suffix = "wow!!!";
-
print str.endswith(suffix);
-
print str.endswith(suffix,20);
-
-
suffix = "is";
-
print str.endswith(suffix, 2, 4);
-
print str.endswith(suffix, 2, 6);
-
This will produce following result:
-
-
True
-
True
-
True
- False
- str.endswith(suffix[, start[, end]])