Files
python-tests/globa_test.py
T

24 lines
707 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
import glob
from pathlib import Path
if __name__ == '__main__':
files = glob.glob("*su*.py")
print(files)
for file in files:
print(f"- {file}")
# iglob 返回的是一个生成器(Generator)。它采用“惰性求值”,
# 只有在你循环遍历它时,它才会去查找下一个文件,极大地节省了内存。
print("-" * 100)
for file in glob.iglob("*su*.py", recursive=True):
print(f"- {file}")
print("-" * 100)
base_dir = Path(".")
# 查找所有的 *su*.py 文件(pathlib 默认支持递归用 rglob
for path in base_dir.rglob("*su*.py"):
print(f"- {path.name} --> {path.absolute()}")