4

os.scandir claims to be a better directory iterator and faster than os.walk(). It became a part of the stdlib for Python 3. Working in production environments, what are the things to consider when moving from os.walk() to os.scandir()?

bossi
  • 1,629
  • 13
  • 17
user1767754
  • 23,311
  • 18
  • 141
  • 164
  • 1
    You could just install 3.5 and use `walk`, which will use the `scandir` algorithm if I recall correctly. – TigerhawkT3 Sep 03 '15 at 23:36
  • 1
    But what when you are using any 2 version (2.7) and want to use os.scandir? – user1767754 Sep 04 '15 at 00:19
  • Python 2.7 is no longer considered production-ready -- no security updates -- so questions about it in a "working in production environments" context are arguably no longer relevant. @bossi, keep in mind that editing an old question brings it back to the front page. You might want to focus on questions that are relevant and valuable today. – Charles Duffy Jul 07 '23 at 16:06

2 Answers2

2

I once used os.scandir() in Python 2.7. It kept on crashing because of weird unicode characters. (ù ỳ ǹ and the likes). Switched back to os.walk() and everything was fine. I would suggest you test that if it's a concern.

Appart from that it really is faster, especially on Windows.

-2

I am not very familiar with Python but recently I have to write some Python 2.x script witch use os.walk() to enumerate large number of files. And in the end I wrote dummy file enumerate on C#. Code:

string dirPath = @"d:\";
DirectoryInfo di = new DirectoryInfo (dirPath)
var fi = di.EnumerateFiles ("*", SearchOption.AllDirectories);

run dummy enumerate and python gets faster :) Note: This is only available in .NET 4.0 and above

Ivan Galabov
  • 1
  • 1
  • 2