Apr-23-2024, 09:14 AM
TypeHints are not required, but they help the developers of libraries to communicate what a method/function is expecting and what it should return.
It does lesser calls and is faster.
This is similar to os.walk, but has some differences with the handling of symlinks.
The last example uses
Quote:What I don't like with the pathlib solution is the call to .is_file() for every path, which does a system call while os.walk() produces the list of files with internal mechanism. This needs to be checked but I suspect that os.walk() does less system calls.
It does lesser calls and is faster.
Output:public@ganymed:~$ for script in walk?.py; do echo -n "$script: "; strace -e newfstatat python3 $script 2>&1 | wc -l; done
walk1.py: 27556
walk2.py: 129893
walk3.py: 11484
Output:public@ganymed:~$ for script in walk?.py; do echo -n "$script: "; time python3 $script; echo ; done
walk1.py:
real 0m0,351s
user 0m0,192s
sys 0m0,160s
walk2.py:
real 0m1,208s
user 0m0,883s
sys 0m0,325s
walk3.py:
real 0m0,285s
user 0m0,179s
sys 0m0,106s
pathlib.Path.walk were added since Python 3.12: https://docs.python.org/3/library/pathli....Path.walkThis is similar to os.walk, but has some differences with the handling of symlinks.
The last example uses
Output:Path.walk()
walk1.pyimport os for root, dirs, files in os.walk("/usr"): for file in files: ...walk2.py
from pathlib import Path for element in Path("/usr").rglob("*"): element.is_file()walk3.py
from pathlib import Path for root, dirs, files in Path("/usr").walk(): for file in files: pass
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!