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))  


[ Python 常見問題 ] How to get symbolic link target in Python?

  Source From  Here Question How do I extract the target path from a  symbolic link ? HowTo The problem with  os .readlink()  is it will onl...