2021年12月21日 星期二

[ Python 常見問題 ] How to use glob() to find files recursively?

 Source From Here

Question
This is what I have:
  1. glob(os.path.join('src','*.c'))  
but I want to search the subfolders of src. Something like this would work:
  1. glob(os.path.join('src','*.c'))  
  2. glob(os.path.join('src','*','*.c'))  
  3. glob(os.path.join('src','*','*','*.c'))  
  4. glob(os.path.join('src','*','*','*','*.c'))  
But this is obviously limited and clunky.

HowTo
Use pathlib.Path.rglob from the the pathlib module, which was introduced in Python 3.5.
  1. from pathlib import Path  
  2.   
  3. for path in Path('src').rglob('*.c'):  
  4.     print(path.name)  
If you don't want to use pathlib, use can use glob.glob('**/*.c'), but don't forget to pass in the recursive keyword parameter and it will use inordinate amount of time on large directories.

For cases where matching files beginning with a dot (.); like files in the current directory or hidden files on Unix based system, use the os.walk solution below. For older Python versions, use os.walk to recursively walk a directory and fnmatch.filter to match against a simple expression:
  1. import fnmatch  
  2. import os  
  3.   
  4. matches = []  
  5. for root, dirnames, filenames in os.walk('src'):  
  6.     for filename in fnmatch.filter(filenames, '*.c'):  
  7.         matches.append(os.path.join(root, filename))  


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...