'File can't be opened when using os.listdir(path)
for file in os.listdir(path):
try:
with open(file, 'r') as fp:
msg = MIMEBase('application', "octet-stream")
msg.set_payload(fp.read())
encoders.encode_base64(msg)
msg.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file))
outer.attach(msg)
except:
print("Unable to open one of the attachments. Error: ", sys.exc_info()[0])
raise
composed = outer.as_string()
I get "FileNotFoundError: [Errno 2] No such file or directory" but the file exists! May be the os.listdir() object type?
Solution 1:[1]
The os.listdir
function returns only the filename. You should concat the path and the file name:
for file in os.listdir(path):
try:
with open(ps.path.join(path, file), 'r') as fp:
...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Luigi D. Teixeira |