Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does os.walk leak memory?

When I run this Python script in Windows, the process grows with no apparent end in sight:

import os

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

Am I misunderstanding something? (I'm using Python 2.7.3.)

like image 1000
user1687699 Avatar asked Sep 21 '12 03:09

user1687699


People also ask

What does os Walk () do?

OS. walk() generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).

Is os walk slow?

Python's built-in os. walk() is significantly slower than it needs to be, because – in addition to calling os. listdir() on each directory – it executes the stat() system call or GetFileAttributes() on each file to determine whether the entry is a directory or not.

What os Walk returns?

os. walk() returns a list of three items. It contains the name of the root directory, a list of the names of the subdirectories, and a list of the filenames in the current directory.

What is os SEP in Python?

Python os.sep() has the following syntax: Copy os.sep. The character used by the operating system to separate pathname components. This is '/' for POSIX and '\\' for Windows. Note that knowing this is not sufficient to be able to parse or concatenate pathnames - use os.


1 Answers

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

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

    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)
like image 130
AAlon Avatar answered Oct 12 '22 07:10

AAlon