Q:

Python not finding file in the same directory

I am writing a simple script that attaches file to a mail, but it is not finding the file. This is my one block:
    # KML attachment
    filename='20140210204804.kml'
    fp = open(filename, "rb")
    att = email.mime.application.MIMEApplication(fp.read(),_subtype="kml")
    fp.close()
    att.add_header('Content-Disposition','attachment',filename=filename)
    msg.attach(att)

The file 20140210204804.kml is present in the same folder as the script. I am getting below error:
 IOError: [Errno 2] No such file or directory: '20140210204804.kml'

Any help is appreciated. 

A:

The working directory is not set to the directory of the script, but to the current directory where you started the script.
Use __file__ to determine the file location and use that as a starting point to make filename an absolute path:
import os

here = os.path.dirname(os.path.abspath(__file__))

filename = os.path.join(here, '20140210204804.kml')

