How to filter files (with known type) from os.walk?

后端 未结 10 1044
北海茫月
北海茫月 2020-12-24 12:10

I have list from os.walk. But I want to exclude some directories and files. I know how to do it with directories:

for root, dirs, files in os.wa         


        
相关标签:
10条回答
  • 2020-12-24 12:29

    Exclude multiple extensions.

    files = [ file for file in files if not file.endswith( ('.dat','.tar') ) ]
    
    0 讨论(0)
  • 2020-12-24 12:31
    files = [ fi for fi in files if not fi.endswith(".dat") ]
    
    0 讨论(0)
  • 2020-12-24 12:38

    A concise way of writing it, if you do this a lot:

    def exclude_ext(ext):
        def compare(fn): return os.path.splitext(fn)[1] != ext
        return compare
    
    files = filter(exclude_ext(".dat"), files)
    

    Of course, exclude_ext goes in your appropriate utility package.

    0 讨论(0)
  • 2020-12-24 12:40

    Another solution would be to use the functions from fnmatch module:

    def MatchesExtensions(name,extensions=["*.dat", "*.txt", "*.whatever"]):
      for pattern in extensions:
        if fnmatch.fnmatch(pattern):
          return True
      return False
    

    This way you avoid all the hassle with upper/lower case extension. This means you don't need to convert to lower/upper when having to match *.JPEG, *.jpeg, *.JPeg, *.Jpeg

    0 讨论(0)
  • 2020-12-24 12:44
    files = [file for file in files if os.path.splitext(file)[1] != '.dat']
    
    0 讨论(0)
  • 2020-12-24 12:52

    All above answers are working. Just wanted to add for anyone else whos files by any chance are coming from heterogeneous sources, e.g. downloading images in archives from the Internet. In this case, because Unix-like systems are case sensitive you may end up having extension like '.PNG' and '.png'. These will be treated by as different strings by endswith method, i.e. '.PNG'.endswith('png') will return False. In order to avoid this problem, use lower() function.

    0 讨论(0)
提交回复
热议问题