os.path.walk problem

Hans Nowak wurmy at earthlink.net
Mon Aug 12 23:55:28 EDT 2002


Terry Hancock wrote:
> Hi all,
> 
> This is a weird bug I just came across, and I'm
> wondering how it can happen. I suppose os.path.walk()
> is actually posixpath.walk() since this is on a
> Linux system. I'm running Python 2.1.3.
> 
> The program is a bulk-loader for Zope which loads
> a set of files from my Zope product into the ZODB.
> The problem isn't on the Zope side, though, but
> on the part where it tries to read the original
> sources.
> 
> What happens is that I get an IOError in the walker
> which complains that I'm trying to read a directory
> as a file  (IOError 21 "Is a directory").  A little
> investigation reveals that this is indeed the case --
> it's trying to read my "CVS" directory.
> 
> Which is odd, because my understanding is that the
> walker is supposed to be guaranteed to be passed
> a directory path and the list of *regular* files in
> that directory (i.e. the latter list shouldn't
> contain directories). 

No, it does include directories. At least on my box it does:

 >>> def visit(arg, dirname, names):
	for name in names:
		fullname = os.path.join(dirname, name)
		if os.path.isdir(fullname):
			print name, "is a directory"

 >>> os.path.walk("p:/projects/kaa", visit, 0)
db_it_lives is a directory
db_test is a directory
db_test04 is a directory
[...etc...]

> Did I misunderstand
> the documentation on os.path.walk()? 

It says:

   The argument dirname specifies the visited directory, the
   argument names lists the files in the directory (gotten from
   os.listdir(dirname)).

[http://www.python.org/doc/current/lib/module-os.path.html]

The wording does suggest that there's only _files_ (and not directories) in the 
list, but then again, os.listdir() returns directories as well as files.

Then it continues by saying:

   The visit function may modify names to influence the set of
   directories visited below dirname, e.g., to avoid visiting
   certain parts of the tree.

This sentence suggests that the list has directories as well.

-- 
Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA=='))
# decode for email address ;-)
The Pythonic Quarter:: http://www.awaretek.com/nowak/




More information about the Python-list mailing list