Is there an os.walk memory leak?

When I run this Python script on Windows, the process grows without a visible end:

import os for i in xrange(1000000): for root, dirs, files in os.walk(r"c:\windows"): pass 

I do not understand something? (I am using Python 2.7.3.)

+4
source share
1 answer

This is due to a memory leak detected in os.path.isdir; see Huge memory leak on os.path.isdir repeated calls? You can verify this yourself using a Unicode encoded path string - there should be no leakage.

os.path.isdir is used in the implementation of os.walk:

  islink, join, isdir = path.islink, path.join, path.isdir try: names = listdir(top) except error, err: if onerror is not None: onerror(err) return dirs, nondirs = [], [] for name in names: if isdir(join(top, name)): dirs.append(name) else: nondirs.append(name) 
+4
source

Source: https://habr.com/ru/post/1435418/


All Articles