作家
登录

你需要知道的、有用的Python功能和特点

作者: 来源: 2017-06-06 12:02:46 阅读 我要评论

大年夜多Python函数有着长且具有描述性的名字。然则定名为glob()的函数你可能不知道它是干什么的除非你大年夜别处已经熟悉它了。

泥像是一个更强大年夜版本的listdir()函数。它可以让你经由过程应用模式匹配来搜刮文件。

你可以像下面如许查找多个文件类型:

  1. import itertools as it, glob 
  2.  
  3.   
  4.  
  5. def multiple_file_types(*patterns): 
  6.  
  7.     return it.chain.from_iterable(glob.glob(pattern) for pattern in patterns) 
  8.  
  9.   
  10.  
  11. for filename in multiple_file_types("*.txt""*.py"): # add as many filetype arguements 
  12.  
  13.     print filename 
  14.  
  15.   
  16.  
  17. output 
  18.  
  19. #=========# 
  20.  
  21. # test.txt 
  22.  
  23. # arg.py 
  24.  
  25. # g.py 
  26.  
  27. # shut.py 
  28.  
  29. # test.py  

如不雅你想获得每个文件的绝对路径,你可以在返回值上调用realpath()函数:

  1. import itertools as it, glob, os 
  2.  
  3.   
  4.  
  5. def multiple_file_types(*patterns): 
  6.  
  7.     return it.chain.from_iterable(glob.glob(pattern) for pattern in patterns) 
  8.  
  9.   
  10.  
  11. for filename in multiple_file_types("*.txt""*.py"): # add as many filetype arguements 
  12.  
  13.     realpath = os.path.realpath(filename) 
  14.  
  15.     print realpath 
  16.  
  17.   
  18.  
  19. output 
  20.  
  21. #=========# 
  22.  
  23. # C:\xxx\pyfunc\test.txt 
  24.  
  25. # C:\xxx\pyfunc\arg.py 
  26.  
  27. # C:\xxx\pyfunc\g.py 
  28.  
  29. # C:\xxx\pyfunc\shut.py 
  30.  
  31. # C:\xxx\pyfunc\test.py  
  1. import glob 
  2.  
  3.   
  4.  
  5. # get all py files 

      推荐阅读

      浅谈Web自适应

    媒介<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> 简单工作简单做-宽度自适应所谓宽度自适应严格来说是一种PC端的自适>>>详细阅读


    本文标题:你需要知道的、有用的Python功能和特点

    地址:http://www.17bianji.com/lsqh/35585.html

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)