charmap codec can't decode byte error with gzipped file in python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: charmap codec can't decode byte error with gzipped file in python (/thread-17931.html) |
charmap codec can't decode byte error with gzipped file in python - bluethundr - Apr-29-2019 I am attempting to send an email that includes a file in gzip format as an attachment using python. But I'm getting an error when I try to send the file. This is the exception I'm getting: Exception: 'charmap' codec can't decode byte 0x90 in position 75: character maps to <undefined>This is what I'm doing in my code: import gzip destination = 'path/to/file' to_addr = input("Enter the recipient's email address: ") from_addr = '[email protected]' subject = "Commpany AWS Billing Info " content = "email to the user about the file" msg = MIMEMultipart() msg['From'] = from_addr msg['To'] = to_addr msg['Subject'] = subject body = MIMEText(content, 'html') msg.attach(body) server = smtplib.SMTP('smtpout.us.companyworld.company.com', 25) filename = destination try: with open(filename, 'r') as f: part = MIMEApplication(f.read(), Name=basename(filename)) part['Content-Disposition'] = 'attachment; filename="{}"'.format(basename(filename)) msg.attach(part) server.send_message(msg, from_addr=from_addr, to_addrs=[to_addr]) print("Email was sent to: %s" % to_addr) except Exception as e: print("Exception: ", e) print("Email was not sent.")The file I am trying to send was created in another function in these lines: import pandas read_sql = 'select * from somedb' mydb = mysql.connector.connect(user='x', password='x',host='x',database='aws_bill') results = pandas.read_sql_query(read_sql, mydb) results.to_csv(destination, index=False, encoding='utf8', compression='gzip')How can I correct this problem and send the attachment? RE: charmap codec can't decode byte error with gzipped file in python - snippsat - Apr-30-2019 You should post all Traceback because there is also line number of error.Also all email imports should be posted. Try line 11: body = MIMEText(content, 'html', 'utf-8')Line 16: with open(filename, 'r', encoding='utf-8') as f: RE: charmap codec can't decode byte error with gzipped file in python - bluethundr - Apr-30-2019 (Apr-30-2019, 04:58 AM)snippsat Wrote: You should post all OK thanks! I will do that next time, and note it going forward that I should post full stack traces. The problem turned out to be that the gzipped file is a binary. So I need to use this option to send it: with open(filename, 'rb') as f: |