I am working on a function that sends emails with an attachment in PYTHON. In order to attach the file, I have to put it with the complete URL from the server, but the name of the file includes the hole URL. How can extrac only the filename from the URL.
I get the file from this URL:
/var/www/RH/HV/FILE.doc
And I want the attachment to appear
FILE.doc
here is the part of the code where i think the instructions should be added…
#adjunto
adjunto = MIMEBase('application', "octet-stream")
adjunto.set_payload(open(file, "rb").read())
encode_base64(adjunto)
adjunto.add_header('Content-Disposition', 'attachment; filename= "%s"' % file)
msg.attach(adjunto)
I hope someone could help!
THKS!!!
Use os.path.basename():
This assumes you’re working with normal file paths, not URLs. But based on the question it looks like you’re not looking at URLs.