python - os.path.getsize(path) or os.stat -
this question has answer here:
what different between os.path.getsize(path)
, os.stat
? 1 best used in python 3? , when use them? , why have 2 same solution? found this answer couldn't understand quote means:
from this, seems pretty clear there no reason expect 2 approaches behave differently (except perhaps due different structures of loops in code)
specifically why have 2 approach , there different?
stat
posix system call (available on linux, unix , windows) returns bunch of information (size, type, protection bits...)
python has call @ point size, there's no system call only size.
so they're same performance-wise (maybe faster stat
that's 1 more function call not i/o related). it's os.path.getsize
simpler write.
that said, able call os.path.getsize
have make sure path file. when called on directory, getsize
returns value (tested on windows) related size of node, have use os.path.isfile
first: call os.stat
.
in end, if want maximize performance, have use os.stat
, check infos see if path file, use st_size
information. way you're calling stat
once.
if you're using os.walk
scan directory, you're exposed more hidden stat
calls, os.scandir
(python 3.5).
related: faster way find large files python?
Comments
Post a Comment