1

Can anyone please help me, I'm a newbie, I have a bit of code which I'm working on and I'm struggling with the file directory path. I have found other examples and tried them as shown below. The Python code is to email out a file called 'myfile.txt' form the folder 'F:\D\OneDrive\Python\Spyder\test'.

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

#sender's address 
fromaddr = "username@gmail.com"
#receiptent's email address
toaddr = "username2@gmail.com"

msg = MIMEMultipart()

msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Python test"

body = "Did it work Sam?"

msg.attach(MIMEText(body, 'plain'))

filename = "myfile.txt"
attachment = open("F:\D\OneDrive\Python\Spyder\test", "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)

msg.attach(part)

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

And I get this error -

PermissionError: [Errno 13] Permission denied:         
  b'F:\\D\\OneDrive\\Python\\Spyder\\test'

If I change the line to -

attachment = open("F:\D\OneDrive\Python\Spyder\test\", "rb")

I get -

attachment = open("F:\D\OneDrive\Python\Spyder\test\", "rb")
                                                          ^
SyntaxError: EOL while scanning string literal

If I change the line to -

attachment = open("F:\\D\\OneDrive\\Python\\Spyder\\test\\", "rb")

I get -

attachment = open("F:\\D\\OneDrive\\Python\\Spyder\\test\\", "rb")

FileNotFoundError: [Errno 2] No such file or directory:         
  'F:\\D\\OneDrive\\Python\\Spyder\\test\\'
9000
  • 39,899
  • 9
  • 66
  • 104
Mark Carr
  • 11
  • 2

3 Answers3

1

If you work in Windows you must use windows path format. Method open with 'rb' parameters read file in byte mode if file is exist. You try read the directory!?

attachment = open('F:\\D\\OneDrive\\Python\\Spyder\\test\\myfile.txt", "rb")

equal

attachment = open(r'F:\D\OneDrive\Python\Spyder\test\myfile.txt', 'rb')

9000
  • 39,899
  • 9
  • 66
  • 104
  • I can see what you mean and I tried them with and without the file name, but both gave me the error - SyntaxError: EOL while scanning string literal. Thanks anyhow – Mark Carr Mar 22 '18 at 07:59
  • ^ Quoting issue here; fixing it. – 9000 Mar 22 '18 at 14:33
0

This represents the path correctly, but fails to provide a file name, because the trailing \ means a directory.

attachment = open("F:\\D\\OneDrive\\Python\\Spyder\\test\\myfile.txt", "rb")

What you likely want is

# Note the r and the lack of a trailing slash.
attachment = open(r"F:\D\OneDrive\Python\Spyder\test\myfile.txt", "rb")
9000
  • 39,899
  • 9
  • 66
  • 104
  • Its making more sense, but I get this error with your solution - PermissionError: [Errno 13] Permission denied: 'F:\\D\\OneDrive\\Python\\Spyder\\test' Thanks anyhow – Mark Carr Mar 22 '18 at 08:00
  • Is this the real path to your file? After re-reading the question, I understood that it's only the path part, and the file is `myfile.txt`. I'm updating my answer. – 9000 Mar 22 '18 at 14:33
0

I have found different code here and this works. Still can't work out why the original code does not work - python program to rename the file with current date in MMDDYYY format and send email with attachment

Fixed code -

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
fromaddr = "username@gmail.com"
toaddr = "username2@gmail.com" 
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Please find the attachment"
body = "HI" 
msg.attach(MIMEText(body, 'plain')) 
filename = "myfile.txt"
#dt = str(datetime.datetime.now())
attachment = open("F:\\D\\OneDrive\\Python\\Spyder\\myfile.txt", "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename) 
msg.attach(part) 
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
Mark Carr
  • 11
  • 2