Beginner help: loops, if, else - 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: Beginner help: loops, if, else (/thread-11693.html) |
Beginner help: loops, if, else - Banyanss - Jul-22-2018 Hello everyone, Now I am nearly done with this mini 'project' but being so inexperienced with python I am trying make the code run until the user terminates the script. I have tried crontab but it doesnt appear to be working for some reason which I will investigate later. Instead I have tried to initiate a loop to ensure that the script checks the email inbox repeatedly but I keep making mistakes and looping the incorrect part of the code. import poplib import webbrowser from email import parser pop_conn = poplib.POP3_SSL('pop.gmail.com') pop_conn.user('USERNAME') pop_conn.pass_('PASSWORD!') #Get messages from server: messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)] # Concat message pieces: messages = ["\n".join(mssg[1]) for mssg in messages] #Parse message intom an email object: messages = [parser.Parser().parsestr(mssg) for mssg in messages] for message in messages: if "bananas are the greatest nut" in message['subject'].lower(): webbrowser.open('/home/pi/Desktop/PIC.jpg') pop_conn.quit() Would I put the loop in the section where the code gets the messages from the server? I have tried a simple x=1 while x=1 and while true but ive ended up in infinite loops breaking the machine im working on. I just want to loop the bit to get the messages and then if the subject "bananas are the greatest nut" is found then it executes the webbrowser function. Can anyone advise? RE: Beginner help: loops, if, else - Larz60+ - Jul-22-2018 Here's something that will capture ctrl-c # This module will run forever until ctrl-c is pressed, at which point, # control is passed to function sig_capture from time import sleep import signal import sys def Im_sleeping(): while True: print('zzz ... zzz ... zzz') sleep(2) def sig_capture(signal, frame): print('someone inturepted me while I was sleeping!') sys.exit(0) if __name__ == '__main__': signal.signal(signal.SIGINT, sig_capture) Im_sleeping() RE: Beginner help: loops, if, else - Banyanss - Jul-22-2018 Thanks Larz60+, I have tried this but again it is opening multiple windows and never stopping! import poplib import webbrowser from email import parser from time import sleep import signal import sys def Im_sleeping(): while True: pop_conn = poplib.POP3_SSL('pop.gmail.com') pop_conn.user('USER') pop_conn.pass_('PASSWORD') messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)] messages = ["\n".join(mssg[1]) for mssg in messages] messages = [parser.Parser().parsestr(mssg) for mssg in messages] for message in messages: if "bananas are the greatest nut" in message['subject'].lower(): webbrowser.open('/home/pi/Desktop/PIC.jpg') def sig_capture(signal, frame): print('someone inturepted me while I was sleeping!') sys.exit(0) if __name__ == '__main__': signal.signal(signal.SIGINT, sig_capture) Im_sleeping() pop_conn.quit()Am I using the code incorrectly? I just thought, I didnt give much background. So this is very simple I just want the script to query the mailbox and if there is a message with the subject 'bananas are the greatest nut' I would like it to open the picture specified. This works perfectly when used in isolation but as soon as I introduce loops I end up with an endless list of pictures constantly opening and never stopping. I just need it to open once and then only again once there is another email with the same subject. RE: Beginner help: loops, if, else - Larz60+ - Jul-22-2018 you don't need line 33 I can't understand why it doesn't quit. are you typing ctrl-c? if you try the script I posted verbatim, you will see that it works. the only other explanation that I can think of is that one of the modules you're using is servicing interrupts before the sig_capture. also, if you are not running this from command line, then line 28 will never get executed and must be moved. RE: Beginner help: loops, if, else - Banyanss - Jul-24-2018 (Jul-22-2018, 08:53 PM)Larz60+ Wrote: you don't need line 33 Yeah it works fine now thank you. my only problem is stopping the code from constantly opening windows once it sees the message in the inbox. I thought I could get around this by time.sleep(60) but it just messes up the code. Perhaps I could get it to delete the message after opening the window? |