# FileNotFoundError: [Errno 2] No such file or directory: 'C'

Hi all,

Newbie here self-learning RF.
I acquired a python code to send email and use it as a library in RF.
If I call the python script directly, the script runs fine.
However, if I call the python function from RF, it returns the error below.

robot --outputdir .\logs\ –exitonfailure .\sendEmail.robot
==============================================================================
Submit Direct Applications :: This .robot file is a suite
==============================================================================
Send Email
sender@email.com - [‘recipient@email.com’] - testsubject - testmessage - [‘C:\Users\\pdf.pdf’]
Send Email | FAIL |
FileNotFoundError: [Errno 2] No such file or directory: ‘C’
------------------------------------------------------------------------------
Submit Email :: This .robot file is a suite | FAIL |
1 test, 0 passed, 1 failed
==============================================================================

Below is my .robot file.

Settings

Library …/python/sendEmail.py

Variables
${sendfrom}= sender@email dot com @{sendto}= recipient@email dot com${subject}= testsubject
${message}= testmessage @{attachments}= C:\Users\\pdf.pdf Test Cases Send Email Log to Console \n${sendfrom} - @{sendto} - ${subject} -${message} - @{attachments}
${isSent}= sendEmail.Send Email${sendfrom} @{sendto} ${subject}${message} @{attachments}
Log To Console \n${isSent} If I set the @{attachments}=${EMPTY}
it will run fine from RF but I will need to send attachments in the email.
I tried Googling but unable to find an answer to my scenario or I am not hitting the correct keywords to search.
I confirm that the file exist, it is on the right path and right permissions.
It looks like there is an issue passing a Windows path as parameter from RF to my python script.
It seems like the error will change base on the first character I set on my @{attachments} variable.
I am out of ideas how to fix it so I will appreciate any help on this.
I’d like this to work if possible and not use any other libraries/function as it works if I call the python script directly.
I just can’t call it from RF .

Below is my sendEmail.py file.

import smtplib
from pathlib import Path
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
from email import encoders

def send_email(send_from, send_to, subject, message, files):
“”“Compose and send email with provided info and attachments.
Args:
send_from (str): from name
send_to (list[str]): to name(s)
subject (str): message title
message (str): message body
files (list[str]): list of file paths to be attached to email
server (str): mail server host name
port (int): port number
username (str): server auth username
password (str): server auth password
use_tls (bool): use TLS mode
“””
server = “smtp.email dot com”
port = 587
username = 'myemail@email dot com
password = ‘mypassword’
use_tls = True

msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject

msg.attach(MIMEText(message))

for path in files:
part = MIMEBase('application', "octet-stream")
with open(path, 'rb') as file:
part.set_payload(file.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename={}'.format(Path(path).name))
msg.attach(part)

smtp = smtplib.SMTP(server, port)
if use_tls:
smtp.starttls()
smtp.login(username, password)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.quit()

Hi Trik,

This line from your .robot file:

try it like this:

@{attachments}=    C:\\Users\\pdf.pdf

Hopefully it’s that simple

Dave.

Hi Dave,

I have that escaped in my .robot file.

I must have removed the extra \ when I edited the file path when I posted it here.

So in short, that is not the issue.

Thanks a lot for the reply.

I was able to find the solution by changing this line from

${isSent}= sendEmail.Send Email${sendfrom} @{sendto} ${subject}${message} @{attachments}

to

${isSent}= sendEmail.Send Email${sendfrom} ${sendto}${subject} ${message}${attachments}

1 Like

When you open a file with the name “filename.ext”; you are telling the open() function that your file is in the current working directory . This is called a relative path.

file = open('filename.ext') //relative path

In the above code, you are not giving the full path to a file to the open() function, just its name - a relative path. The error “FileNotFoundError: [Errno 2] No such file or directory” is telling you that there is no file of that name in the working directory. So, try using the exact, or absolute path.

file = open(r'C:\path\to\your\filename.ext') //absolute path

In the above code, all of the information needed to locate the file is contained in the path string - absolute path.

If the user does not pass the full path to the file (on Unix type systems this means a path that starts with a slash), the python file path is interpreted relatively to the current working directory. The current working directory usually is the directory in which you started the program. In order to make this work, the directory containing the python executable must be in the PATH, a so-called environment variable that contains directories that are automatically used for searching executables when you enter a command. In any case, if your Python script file and your data input file are not in the same directory, you always have to specify either a relative path between them or you have to use an absolute path for one of them.