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.)
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).
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With