Python Forum
charmap codec can't decode byte error with gzipped file in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
charmap codec can't decode byte error with gzipped file in python
#1
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?
Reply
#2
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:
Reply
#3
(Apr-30-2019, 04:58 AM)snippsat Wrote: 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:

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:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Search for multiple unknown 3 (2) Byte combinations in a file. lastyle 7 1,256 Aug-14-2023, 02:28 AM
Last Post: deanhystad
  UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd2 in position 16: invalid cont Melcu54 3 4,697 Mar-26-2023, 12:12 PM
Last Post: Gribouillis
  Decode string ? JohnnyCoffee 1 785 Jan-11-2023, 12:29 AM
Last Post: bowlofred
  [SOLVED] [Debian] UnicodeEncodeError: 'ascii' codec Winfried 1 988 Nov-16-2022, 11:41 AM
Last Post: Winfried
  UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 34: character Melcu54 7 18,303 Sep-26-2022, 10:09 AM
Last Post: Melcu54
  Byte Error when working with APIs Oshadha 2 980 Jul-05-2022, 05:23 AM
Last Post: deanhystad
  ASCII-Codec in Python3 [SOLVED] AlphaInc 4 5,984 Jul-07-2021, 07:05 PM
Last Post: AlphaInc
  UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 error from Mysql call AkaAndrew123 1 3,383 Apr-28-2021, 08:16 AM
Last Post: AkaAndrew123
  JSON Decode error when using API to create dataframe Rubstiano7 4 2,879 Jan-11-2021, 07:52 PM
Last Post: buran
  Skeleton file export error Python Code pepapoha 4 3,426 Nov-17-2020, 02:06 AM
Last Post: pepapoha

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020