empty csv file attachments

B

Bobby Roberts

hi group.

I'm new to python but a veteran at programming. 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:
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
attachments.append('/'.join((ctx.request.library,
myfilename)))

[snip... sends email just after this]

any ideas?
 
S

Sean DiZazzo

hi group.

I'm new to python but a veteran at programming.  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:
            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
        attachments.append('/'.join((ctx.request.library,
myfilename)))

 [snip... sends email just after this]

any ideas?

I would sprinkle some print statements in there to narrow it down...

~Sean
 
P

Peter Otten

Bobby said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top