Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1081933
  • 博文数量: 110
  • 博客积分: 10068
  • 博客等级: 上将
  • 技术积分: 1505
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-25 11:28
文章分类

全部博文(110)

文章存档

2011年(4)

2010年(5)

2009年(73)

2008年(28)

我的朋友

分类: Python/Ruby

2011-12-29 02:45:23

源代码参考_Python_cookbook
代码如下:
  1. import os, itertools
  2. def anyTrue(predicate,sequence):
  3.     return True in itertools.imap(predicate,sequence)

  4. def endsWith(s,*endings):
  5.     return anyTrue(s.endswith,endings)
  6. '''
  7. s.endswith 是一个方法,判断后缀名是否是在指定的endings里,如果是返回True
  8. anyTrue(s.endswith,endings) 将这个方法传递给anyTrue,
  9. itertools.imap(predicate,sequence) 就相当于filename.endswith(enddings)
  10. 返回结果为True就执行print filename
  11. '''

  12. for filename in os.listdir('.'):
  13.     if endsWith(filename,'.jpg','.jpeg','.gif'):
  14.         print filename

  15. endswith判断文件后缀名是否在列表当中
    1. str.endswith(suffix[, start[, end]])
    2. 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.


    3. Example:

    4. #!/usr/bin/python

    5. str = "this is string example....wow!!!";

    6. suffix = "wow!!!";
    7. print str.endswith(suffix);
    8. print str.endswith(suffix,20);

    9. suffix = "is";
    10. print str.endswith(suffix, 2, 4);
    11. print str.endswith(suffix, 2, 6);
    12. This will produce following result:

    13. True
    14. True
    15. True
    16. False
阅读(5073) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~