Python Forum
code works, but how? What does the last line do?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code works, but how? What does the last line do?
#1
I have to download 130 student photos from my email box. I am trying to get Python to do this. I can only dig up bits of code from the internet, patch them together and hope it works.

I will make a loop, but I first tried with just 1 email. This code works.

#! /usr/bin/python3
# should get image attachments from emails

import sys
import pyzmail
import email
from imapclient import IMAPClient


HOST = 'imap.qq.com'
USERNAME = '[email protected]'
PASSWORD = 'myIMAPpassword'


pathToFiles = '/home/pedro/getEmailtexts/emailTexts/'
server = IMAPClient(HOST, use_uid=True, ssl=True)
server.login(USERNAME, PASSWORD)
select_info = server.select_folder('Inbox')

rawMessage = server.fetch([59], ['BODY[]', 'FLAGS']) # I know that UID 59 has a photo, I sent it.

message = pyzmail.PyzMessage.factory(rawMessage[59][b'BODY[]'])
len(message.get_payload()) # response is 2
attachment = message.get_payload()[1]
attachment.get_content_type() # response is 'image/jpeg'
open(pathToFiles + 'rob.jpg', 'wb').write(attachment.get_payload(decode=True)) # saves the photo to my computer
Could someone please elaborate in the last line? I like to try to understand what is going on (I don't really understand).
Can this be broken into steps? What is 'wb'?
Reply
#2
the line is commented and is doing exactly that - save the photo (i.e. the attachment) to hard drive at pathToFiles
it is same as
with open(pathToFiles + 'rob.jpg', 'wb') as f:
    f.write(attachment.get_payload(decode=True))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to understand the meaning of the line of code. jahuja73 0 305 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 1,009 Nov-15-2023, 06:56 PM
Last Post: jst
  Code works but doesn't give the right results colin_dent 2 715 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  Code used to work 100%, now sometimes works! muzicman0 5 1,444 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,672 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Pandas - error when running Pycharm, but works on cmd line zxcv101 1 1,367 Jun-18-2022, 01:09 PM
Last Post: snippsat
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,402 Feb-13-2022, 07:13 PM
Last Post: menator01
  Pyspark - my code works but I want to make it better Kevin 1 1,786 Dec-01-2021, 05:04 AM
Last Post: Kevin
Question email code works in 2.7 but not in 3 micksulley 3 2,584 Nov-04-2021, 09:44 PM
Last Post: micksulley
  My simple code don't works !! Nabi666 1 1,604 Sep-06-2021, 12:10 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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