title: python run some code before running another
body: <p>I am very new to python programming, please go easy on me!  </p>
  
  <p>I am querying my MySQL database and writing output to a file and sending an email of the results.  However the email is being sent before the file is written to.  How do I tell my code to do the query and writing to file <strong>before</strong> sending the email?</p>
  
  <pre><code>#!/usr/bin/python
  # Import smtplib for the actual sending function
  import smtplib
  import MySQLdb as mdb
  import sys
  import csv
  
  con = mdb.connect('localhost', 'myuser', 'mypassword', 'mydatabase');
  with con:
      cur = con.cursor()
      cur.execute("SELECT * from vw_mail")
      rows = cur.fetchall()
  c = csv.writer(open('/home/pi/mail.csv','wb'))
  c.writerows(rows)
  
  # Import the email modules we'll need
  from email.mime.text import MIMEText
  
  # Open a plain text file for reading.  For this example, assume that
  # the text file contains only ASCII characters.
  fp = open('/home/pi/mail.csv','rb')
  # Create a text/plain message
  msg = MIMEText(fp.read())
  fp.close()
  
  # me == the sender's email address
  # you == the recipient's email address
  msg['Subject'] = 'MySubject'
  msg['From'] = 'me@me.com'
  msg['To'] = 'you@you.com'
  
  # Send the message via our own SMTP server, but don't include the
  # envelope header.
  s = smtplib.SMTP('smtp.me.com')
  s.sendmail('me@me.com','you@you.com', msg.as_string())
  s.quit() 
  </code></pre>