[Python-ideas] Speed up os.walk() 5x to 9x by using file attributes from FindFirst/NextFile() and readdir()

Ben Hoyt benhoyt at gmail.com
Thu Nov 15 22:50:32 CET 2012


>> Not currently. I haven't thought about this too hard -- there way be a
>> bit that's always set/not set within st_mode itself. Otherwise I'd
>> have to add a full_st_mode or similar property
>
> Why not just add d_type?

It's not a bad idea, but neither am I super-keen on it, because of
these two reasons:

1) You've have to add a whole new way / set of constants / functions
to test for the different values of d_type. Whereas there's already
stuff (stat module) to test for st_mode values.

2) It'd make the typical use case more complex, for example, the
straight "if st.st_mode is None ... else ..." I gave earlier becomes
this:

for filename, st in iterdir_stat(path):
     if st.d_type is None:
          if st.st_mode is None:
               st = os.stat(os.path.join(path, filename))
          is_dir = stat.S_ISDIR(st.st_mode)
     else:
          is_dir = st.d_type == DT_DIR

-Ben



More information about the Python-ideas mailing list