os.path.walk problem

Terry Hancock hancock at anansispaceworks.com
Tue Aug 13 03:47:08 CEST 2002


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). Now I suppose I can fix it by
testing the files before operating on them, but I
thought this was pretty odd.  Did I misunderstand
the documentation on os.path.walk()? Is there some
normal (or even abnormal) condition under which
it is expected to let a directory slip through as
a regular file? Is it relevant that "CVS" is the
*only* directory in this path?

To make matters worse, this code *works* on the
FreeBSD server at Imeme, but not on my Linux server
at home (it's Debian Potato, so kernal 2.2.17, I
think).  Curiouser and curiouser.

Anyway, here's a copy of the affected code. The
program calls DocInstaller() on my "dtml/Legal" directory
which contains license files and stuff and is
supposed to load them into Zope as DTML Documents. It
then calls DocWalker() recursively through os.path.walk().
I only just added the try-except clause to find out what
was choking it. It is indeed the directory "CVS". You'll
note that I trapped the case of DocWalker being called
with "CVS" as the directory path, but not the case of it
appearing in the file list (because I expected that not
to ever happen).

# Text (DTML Document) Loader
#---------------------------------------------------------------------------
# So far, used only for the Legal Documents Folder (v1.0)
#---------------------------------------------------------------------------

def DocWalker( (obj, basedir), srcpath, docs):
	"""
	Walker to install Text files as DTML Documents, 
	beneath the object obj.
	"""
	# Ignore inappropriate directories (such as the CVS directory):
	if os.path.split(srcpath)[1] in ('CVS',):
	    return
    
	# Create the directory:
	src_l   = string.split(srcpath, '/')
	tgtpath = string.join(src_l[src_l.index(basedir):] ,'/')
	fdrpath, newfdr = os.path.split(tgtpath)
	
	if not fdrpath:
		obj.manage_addFolder(newfdr)
	else:
		obj.unrestrictedTraverse(fdrpath).manage_addFolder(newfdr)
	fdr = obj.unrestrictedTraverse(tgtpath)
	
	# Populate with DTML Documents:
	for doc in docs:
	  try:
	    docpath = os.path.join(srcpath, doc)

	    docfile = open(docpath)
	    docdata = docfile.read()
	    docfile.close()
	  except:
	    raise IOError, "docpath='%s'" % (docpath,)
	  fdr.manage_addDocument(doc, '', file=docdata)

	    
def DocInstaller(obj, path, basedir):
	"""
	Install a tree of documents as DTML Documents.
	"""
	text_directory = os.path.join(package_home(globals()), path)
	os.path.walk(text_directory, DocWalker, (obj, basedir))

Any ideas?

Thanks,
Terry

-- 
------------------------------------------------------
Terry Hancock
hancock at anansispaceworks.com       
Anansi Spaceworks                 
http://www.anansispaceworks.com 
P.O. Box 60583                     
Pasadena, CA 91116-6583
------------------------------------------------------




More information about the Python-list mailing list