python script to upload file with dynamically generated filename
I'm working on a script to upload a file to a file repository (zope folder) and generate a custom file name. My intention is to use a LocalFS object (instead of the Zope folder) once this bit is sorted. My goal is to rename the file based on the filename and extension variables that I'm passing to the script. The part I'm stuck on is the syntax of this part: manage_addFile(id='%s%s % (filename, extension)', title='', file=file) In this example I'd like to set the id to 'thefile.jpg' TIA, David +++++++++++++ [ script ] +++++++++++++++ #parameters file, filename='thefile', extension='.jpg' """ Uploads a file to the repository with a crafted id. """ from Products.PythonScripts.standard import url_quote # crafts the filename and creates the file container.Files.manage_addProduct['OFSP'].manage_addFile(id='%s%s % (filename, extension)', title='', file=file) # creates the success message message="The file '%s' has been added to the repository." % file.filename # redirects to the file repository interface page return context.REQUEST.RESPONSE.redirect("%s/repository_html?message=%s" % (container.absolute_url(), url_quote(message)))
filename = 'thefile' extension = '.jpg' manage_addFile(id='%s%s % (filename, extension)', title='', file=file) On Mon, 2003-07-28 at 17:56, David Siedband wrote:
I'm working on a script to upload a file to a file repository (zope folder) and generate a custom file name. My intention is to use a LocalFS object (instead of the Zope folder) once this bit is sorted.
My goal is to rename the file based on the filename and extension variables that I'm passing to the script.
The part I'm stuck on is the syntax of this part: manage_addFile(id='%s%s % (filename, extension)', title='', file=file)
In this example I'd like to set the id to 'thefile.jpg'
TIA,
David
+++++++++++++ [ script ] +++++++++++++++
#parameters file, filename='thefile', extension='.jpg'
""" Uploads a file to the repository with a crafted id. """ from Products.PythonScripts.standard import url_quote
# crafts the filename and creates the file container.Files.manage_addProduct['OFSP'].manage_addFile(id='%s%s % (filename, extension)', title='', file=file)
# creates the success message message="The file '%s' has been added to the repository." % file.filename
# redirects to the file repository interface page return context.REQUEST.RESPONSE.redirect("%s/repository_html?message=%s" % (container.absolute_url(), url_quote(message)))
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev ) -- Edward Muller
Interlix (http://www.interlix.com) Phone: 417-862-0573 - Cell: 417-844-2435 - Fax: 417-862-0572 Web Hosting - PC Service & Support - Custom Programming - Network Service & Support Specializing in Open Source Solutions
The part I'm stuck on is the syntax of this part: manage_addFile(id='%s%s % (filename, extension)', title='', file=file)
The string syntax for id is wrong. Try manage_addFile(id='%s%s' % (filename, extension), title='', file=file) instead. The percent is an operator on the string. It's also nice to tell us what error you get rather than pose a find-whatever-is-wrong-with-this-code problem. --jcc -- "My point and period will be throughly wrought, Or well or ill, as this day's battle's fought."
participants (3)
-
David Siedband -
Edward Muller -
J Cameron Cooper