[Zope] add file to zope filesystem unless it exists

John Hunter jdhunter@ace.bsd.uchicago.edu
Wed, 13 Feb 2002 16:00:22 -0600


I want to have a user upload a file to the zope filesystem, but if the
file already exists I want to ignore the request

I have a Python Script

## Script (Python) "add_to_filesystem_py"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=folder_name,file_name
##title=
##
REQUEST = container.REQUEST
RESPONSE =  REQUEST.RESPONSE

idFolder = getattr(context.Patients, REQUEST.get('id'))
folder = getattr(idFolder, folder_name)

if not hasattr(folder, file_name):
    folder.manage_addFile(id='', file=file_name)

return getattr(file_name, 'filename')



Without the 'if not hasattr(folder, file_name):' the script works fine
and adds the file to the filesystem and returns an error if the file
already exists there.  Apparently I am not using the hasattr part
correctly.  What is the correct way in a python script to test for the
existence of a file in a folder object?

Thanks,
John Hunter