empty csv file attachments

Peter Otten __peter__ at web.de
Thu Sep 25 03:52:55 EDT 2008


Bobby Roberts wrote:

> I'm new to python but a veteran at programming.  

Hm, your code doesn't show that. The time to read the tutorial would be time
well spend. After that, a quick look at what the standard library has to
offer wouldn't hurt. E. g. reading/writing CSV files is a solved problem in
python ;) 

> This one has me 
> stumped.  I have a simple contact form which the user fills out.  The
> email is sent to the site user as well and it is delivered with the
> content in the body of the email as well in nice order.  I have
> modified my code to also send the content as a csv attachment.  On the
> server, the file is perfectly generated with content.  The attachment,
> however, is completely blank.  Any ideas what that could be?  My code
> snippet is shown below:
> 
> 
>       if int(attachmenttype)==2 or int(attachmenttype)==3:
>         for field in ctx.request.field_names():
>           if field=='last_name':
>             myfilename=ctx.request.field_value(field)+'.txt'
>         if myfilename=='':
>           myfilename='tempfile.txt'
>         mypath= mynewfilepath + '/' + myfilename
>         f=open(mypath, 'w')
>         mynewstring=''
>         counter=0
>         for field in ctx.request.field_names():
>           if field != 'inquiry_required':
>             mynewstring=mynewstring + field +','
>         if mynewstring[-1]==',':
>           mynewstring=mynewstring[0:len(mynewstring)-1]
>         f.write(mynewstring)
>         f.write ('\n')
> 
>         mynewstring=''
>         counter=1
>         for field in ctx.request.field_names():
>           fielddata=ctx.request.field_value(field)
>           if counter==1:

Hm, above you skip the field "inquiry_required", here you skip the second
field.

>             dummydata=0
>           else:
>             mynewstring=mynewstring + '"' + fielddata.replace('"','')
> + '",'
>           counter = counter + 1
>         if mynewstring[-1]==',':
>           mynewstring=mynewstring[0:len(mynewstring)-1]
>         f.write(mynewstring)
>         f.write('\n')
>         f.close

Your actual problem might be that f.close doesn't close (and therefore
flush) the file, you need to call it with f.close().

>         attachments.append('/'.join((ctx.request.library,
> myfilename)))

Hm, is ctx.request.library the same as mynewfilepath?

With some guessing your code becomes (untested)

import csv
import os

if int(attachmenttype) in (2, 3):
    filename = 'tempfile.txt'

    if "last_name" in ctx.request.field_names():
        last_name = ctx.request.field_value("last_name")
        if last_name:
            filename = last_name + ".txt"

    path = os.path.join(ctx.request.library, filename)

    f = open(path, 'wb')
    writer = csv.writer(f)
    fieldnames = [field for field in ctx.request.field_names()
                  if field != "inquiry_required"]
    writer.writerow(fieldnames)
    writer.writerow(ctx.request.field_value(field) for field in fieldnames)
    f.close()
    attachments.append(path)

The code to build the filename is still clumsy, but to do better I'd have to
know the library you are using. Personally I'd always use "tempfile.txt"
and be done. This would also avoid fun with last names
like ../../just_testing_file_permissions.

Peter



More information about the Python-list mailing list