Categories
Python Answers

How to send email attachments with Python?

Sometimes, we want to send email attachments with Python.

In this article, we’ll look at how to send email attachments with Python.

How to send email attachments with Python?

To send email attachments with Python, we can use the smtplib library.

For instance, we write

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Encoders


SUBJECT = "Email Data"

msg = MIMEMultipart()
msg['Subject'] = SUBJECT 
msg['From'] = self.EMAIL_FROM
msg['To'] = ', '.join(self.EMAIL_TO)

part = MIMEBase('application', "octet-stream")
part.set_payload(open("text.txt", "rb").read())
Encoders.encode_base64(part)
    
part.add_header('Content-Disposition', 'attachment; filename="text.txt"')

msg.attach(part)

server = smtplib.SMTP(self.EMAIL_SERVER)
server.sendmail(self.EMAIL_FROM, self.EMAIL_TO, msg.as_string())

to create the attachment with the MIMEBase class.

And then we call set_payload with the file we want to attach.

Next, we encode the attachment to a base64 string with Encoders.encode_base64.

And then we add the Content-Disposition header with add_header.

And then we call msg.attach with part to attach the attachment to the message.

Then we call sendmail to send the email with the attachment.

Conclusion

To send email attachments with Python, we can use the smtplib library.

Categories
Python Answers

How to capture SIGINT in Python?

Sometimes, we want to capture SIGINT in Python.

In this article, we’ll look at how to capture SIGINT in Python.

How to capture SIGINT in Python?

To capture SIGINT in Python, we can use the signal.signal method.

For instance, we write

import signal
import sys

def signal_handler(sig, frame):
    print('You pressed Ctrl+C!')
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)
print('Press Ctrl+C')
signal.pause()

to call signal.signal with signal.SIGINT and signal_handler to use the signal_handler function to handle the SIGINT event.

In it, we call sys.exit with 0 to exit the script with code 0.

Conclusion

To capture SIGINT in Python, we can use the signal.signal method.

Categories
Python Answers

How to print in one line dynamically with Python?

Sometimes, we want to print in one line dynamically with Python.

In this article, we’ll look at how to print in one line dynamically with Python.

How to print in one line dynamically with Python?

To print in one line dynamically with Python, we can use stdout.write.

For instance, we write

from sys import stdout
from time import sleep

for i in range(1, 20):
    stdout.write('\r%d' % i)
    stdout.flush()
    sleep(1)
stdout.write('\n')

to call stdout.write with the \r character to overwrite the standard out with i.

Then we call flush to update the screen.

Conclusion

To print in one line dynamically with Python, we can use stdout.write.

Categories
Python Answers

How to get a substring of a string in Python?

Sometimes, we want to get a substring of a string in Python.

In this article, we’ll look at how to get a substring of a string in Python.

How to get a substring of a string in Python?

To get a substring of a string in Python, we can use the slice syntax.

For instance, we write

y = x[2:-2]

to get the substring from string x from index 2 to the 2nd last character in the string.

We can also skip the start or end indexes.

For instance, we write

y = x[2:]

to get the character at index 2 to the end of the string.

And we write

x[:-2]

to get the character at index 0 to the 2nd last character of the string.

Conclusion

To get a substring of a string in Python, we can use the slice syntax.

Categories
Python Answers

How to swap two variables in Python?

Sometimes, we want to swap two variables in Python.

In this article, we’ll look at how to swap two variables in Python.

How to swap two variables in Python?

To swap two variables in Python, we can a tuple to another tuple.

For instance, we write

(a, b) = (b, a)

to put a and b in a tuple with (b, a).

Then we unpack the values and assign them to a and b.

Conclusion

To swap two variables in Python, we can a tuple to another tuple.