Mark Carr
Mark Carr

Reputation: 11

windows file directory path in Python

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 = "[email protected]"
#receiptent's email address
toaddr = "[email protected]"

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\\'

Upvotes: 1

Views: 1635

Answers (3)

Mark Carr
Mark Carr

Reputation: 11

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 = "[email protected]"
toaddr = "[email protected]" 
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()

Upvotes: 0

Aleksandr Fuze
Aleksandr Fuze

Reputation: 336

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')

Upvotes: 1

9000
9000

Reputation: 40904

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")

Upvotes: 0

Related Questions