0

I have a mbox file that contains many emails,I tried this code that typically reads all messages,what i want is to read and print only the last email of the mbox file and store it alone in another mbox file. This is the simple code that I wrote:

import mailbox

for msg in mailbox.mbox('C:\\Users\\hmk\Desktop\\PFE 2019\\ML\\MachineLearningPhishing-master\\MachineLearningPhishing-master\\code\\resources\\mboxfile.mbox'):
print(msg)
mamadou
  • 135
  • 2
  • 13

1 Answers1

2

Your code has a syntax error; the line after the for loop should be indented. But actually the solution to your problem is to move it outside the loop. Then you just need to put something else inside the loop.

import mailbox

for msg in mailbox.mbox('C:\\Users\\hmk\Desktop\\PFE 2019\\ML\\MachineLearningPhishing-master\\MachineLearningPhishing-master\\code\\resources\\mboxfile.mbox'):
    pass
# We are now outside the loop, and `msg` contains the last message
print(msg)

Of course, a better fix is to not loop at all.

messages = mailbox.mbox('C:\\Users\\hmk\Desktop\\PFE 2019\\ML\\MachineLearningPhishing-master\\MachineLearningPhishing-master\\code\\resources\\mboxfile.mbox')
print(messages[messages.keys()[-1]])

The above assumes you are on a Python version which is new enough to keep dictionaries sorted in insertion order. If not, you probably do need the loop after all.

As a final aside, probably don't hardcode absolute file paths. Make your program accept a file name argument, so you can run it on any mbox file in any directory.

import mailbox
import sys

messages = mailbox.mbox(sys.argv[1])
print(messages[messages.keys()[-1]])

Call it like

python3 lastmsg.py C:\Users\hmkDesktop\PFE 2019\ML\MachineLearningPhishing-master\MachineLearningPhishing-master\code\resources\mboxfile.mbox >last

Obviously, a production script should have some error checking and help etc but I leave those as an exercise.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    On Python 3.7, using `messages[-1]` raises a `KeyError`. Using `messages[len(messages)-1]` works. – Roland Smith Aug 04 '19 at 21:20
  • Thanks, and for the part where i should call it how can i do it using a batch script – mamadou Aug 05 '19 at 12:12
  • Friends don't let friends use Windows but that should be completely trivial. Quick googling got me https://stackoverflow.com/questions/21582886/creating-a-windows-batch-file-that-accepts-parameters – tripleee Aug 05 '19 at 12:15
  • But let me figure out Roland's comment, he is right, it returns a hash not a list. – tripleee Aug 05 '19 at 12:17
  • When I executed it on jupyter notebook it worked fine, but when i used batch script it gave me an error `Key Error: ne message with key -1` – mamadou Aug 05 '19 at 12:19
  • Yeah, that's exactly what my previous comment talks about. See updated answer now. – tripleee Aug 05 '19 at 12:21
  • @tripleee yes i tried it with you syntax in a batch file and it didn't work Here is my batch script `@echo off set VAR_1= "C:\Users\hmk\Desktop\PFE 2019\ML\MachineLearningPhishing-master\MachineLearningPhishing-master\code\resources\email-enron.mbox" python get_last_mail.py %1 %VAR_1 pause` – mamadou Aug 05 '19 at 12:23
  • Probably ask a new question if you can't figure it out. I am the only one who sees your comments here and I really can't help with `cmd` questions. Putting code in comments doesn't work very well, as you can see; though I guess the space after the equals sign might be a problem. – tripleee Aug 05 '19 at 12:24