Construct path
Make the filename an object instead of strings.
|
|
Iterate dir
-
.iterdir()method iterates over all the files in the given directory1 2 3 4 5from pathlib import Path from collections import Counter # count number of files of different types Counter(path.suffix for path in Path.cwd().iterdir())Return:
Counter({'.md': 2, '.txt': 4, '.pdf': 2, '.py': 1}) -
.glob("*.txt")returns all the files with a.txtsuffix in the current directory.1 2>>> Counter(path.suffix for path in Path.cwd().glob("*.p*")) Counter({'.pdf': 2, '.py': 1}) -
.rglob()recursively find all the files in both the directory and its subdirectories.1 2 3 4 5 6def tree(Path(directory)): print(f"+ {directory}") for path in sorted(directory.rglob("*")): depth = len(path.relative_to(directory).parts) spacer = " " * depth print(f"{spacer}+ {path.name}")
glob two patterns
python - How to glob two patterns with pathlib? - Stack Overflow
|
|