![]() |
NameError: name '...........' is not defined - 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: NameError: name '...........' is not defined (/thread-4317.html) Pages:
1
2
|
NameError: name '...........' is not defined - new2py - Aug-07-2017 Hello I am very new to Python and need help with this script I found online. Every time I run it I get this error in PyCharm FOLDER = EMPLOYMENT NameError: name 'EMPLOYMENT' is not defined The script is supposed to get all the email addresses in my gmail account and put them in a folder #!/usr/bin/python import imaplib import sys import email import re FOLDER = sys.argv[0] FOLDER = EMPLOYMENT LOGIN = yourusername PASSWORD = yourpassword IMAP_HOST = imap.gmail.com # Change this according to your provider email_list = [] email_unique = [] mail = imaplib.IMAP4_SSL(IMAP_HOST) mail.login(LOGIN, PASSWORD) mail.select(FOLDER) result, data = mail.search(None, 'ALL') ids = data[0] id_list = ids.split() for i in id_list: typ, data = mail.fetch(i,'(RFC822)') for response_part in data: if isinstance(response_part, tuple): msg = email.message_from_string(response_part[1]) sender = msg['from'].split()[-1] address = re.sub(r'[<>]','',sender) # Ignore any occurences of own email address and add to list if not re.search(r'' + re.escape(LOGIN),address) and not address in email_list: email_list.append(address) print(address) RE: NameError: name '...........' is not defined - ichabod801 - Aug-07-2017 On line 9, you have EMPLOYMENT. That is a variable name, and you have not defined a value for it by line 9. Variable names have to be on the left side of an assignment before they can be on the right side of an assignment. script runs but printed nothing, please help! - new2py - Aug-08-2017 Hi I'm new at this as this is my first python script I found it online. The program below is supposed to get all the email addresses from my inbox and save them to a folder named EMPLOYMENT. I got the program to run with no errors: Process finished with exit code 0 but nothing happened, I didn't see one single email address. Could you please help me understand what I'm missing? #!/usr/bin/python import imaplib import sys import email import re FOLDER = sys.argv[0] FOLDER = "EMPLOYMENT" LOGIN = "[email protected]" PASSWORD = "yourpassword" IMAP_HOST = "imap.gmail.com" # Change this according to your provider email_list = [] email_unique = [] mail = imaplib.IMAP4_SSL(IMAP_HOST) mail.login(LOGIN, PASSWORD) mail.select(FOLDER) result, data = mail.search(None, 'ALL') ids = data[0] id_list = ids.split() for i in id_list: typ, data = mail.fetch(i,'(RFC822)') for response_part in data: if isinstance(response_part, tuple): msg = email.message_from_string(response_part[1]) sender = msg['from'].split()[-1] address = re.sub(r'[<>]','',sender) # Ignore any occurences of own email address and add to list if not re.search(r'' + re.escape(LOGIN),address) and not address in email_list: email_list.append(address) print(address) RE: NameError: name '...........' is not defined - hbknjr - Aug-08-2017 As the other answer suggested, you may want to assign value to EMPLOYMENT before line 9. Or put 'EMPLOYMENT' in quotes if its the folder name RE: script runs but printed nothing, please help! - buran - Aug-08-2017 (Aug-08-2017, 03:39 AM)new2py Wrote: The program below is supposed to get all the email addresses from my inbox and save them to a folder named EMPLOYMENT Actually it is looking for folder EMPLOYMENT in your mailbox and should search emails in this folder. I would comment out line#19 (the default is INBOX) and see what would happen. Note that it will just print e-mail addresses in the console. You are doing nothing with email_list that is created RE: NameError: name '...........' is not defined - new2py - Aug-08-2017 Thanks, I added quotes to identify the names, the script ran successfully, but nothing happened, it did nothing. RE: NameError: name '...........' is not defined - nilamo - Aug-08-2017 (Aug-08-2017, 03:39 AM)new2py Wrote: result, data = mail.search(None, 'ALL') That's line 21. If you add some print functions, do you get any output? result, data = mail.search(None, 'ALL') print(result) print(data) RE: NameError: name '...........' is not defined - new2py - Aug-09-2017 I added the following like you said and ran in PyCharm: #!/usr/bin/python import imaplib import sys import email import re FOLDER = sys.argv[0] FOLDER = "EMPLOYMENT" LOGIN = "emailaddresshere" PASSWORD = "passwordhere" IMAP_HOST = "imap.gmail.com" # Change this according to your provider email_list = [] email_unique = [] mail = imaplib.IMAP4_SSL(IMAP_HOST) mail.login(LOGIN, PASSWORD) mail.select(FOLDER) result, data = mail.search(None, 'ALL') print(result) print(data) ids = data[0] id_list = ids.split() for i in id_list: typ, data = mail.fetch(i,'(RFC822)') for response_part in data: if isinstance(response_part, tuple): msg = email.message_from_string(response_part[1]) sender = msg['from'].split()[-1] address = re.sub(r'[<>]','',sender) # Ignore any occurences of own email address and add to list if not re.search(r'' + re.escape(LOGIN),address) and not address in email_list: email_list.append(address) print(address)and nothing happened all i got was this: C:\Users\mattis\AppData\Local\Programs\Python\Python36\python.exe C:/Users/mattis/Documents/email_addy_extrac.py OK [b''] Process finished with exit code 0 Python guru's, my 1st Python script, need HELP - new2py - Aug-09-2017 I found this script online that is supposed to get all the email addresses in my Inbox and print them to a folder, it successfully ran in PyCharm but nothing happened. I didn't see any email addresses, please help ![]() #!/usr/bin/python import imaplib import sys import email import re FOLDER = sys.argv[0] FOLDER = "EMPLOYMENT" LOGIN = "emailaddresshere" PASSWORD = "passwordhere" IMAP_HOST = "imap.gmail.com" # Change this according to your provider email_list = [] email_unique = [] mail = imaplib.IMAP4_SSL(IMAP_HOST) mail.login(LOGIN, PASSWORD) mail.select(FOLDER) result, data = mail.search(None, 'ALL') ids = data[0] id_list = ids.split() for i in id_list: typ, data = mail.fetch(i,'(RFC822)') for response_part in data: if isinstance(response_part, tuple): msg = email.message_from_string(response_part[1]) sender = msg['from'].split()[-1] address = re.sub(r'[<>]','',sender) # Ignore any occurences of own email address and add to list if not re.search(r'' + re.escape(LOGIN),address) and not address in email_list: email_list.append(address) print(address)result after running the script: Process finished with exit code 0 RE: NameError: name '...........' is not defined - nilamo - Aug-09-2017 (Aug-09-2017, 05:43 PM)new2py Wrote: OKSo you're communicating fine with the imap server, but it's telling you that there's no messages in the EMPLOYMENT folder. Maybe try mail.list() (https://docs.python.org/3.6/library/imaplib.html#imaplib.IMAP4.list) so you can see what it is, exactly, that the server recognizes as folder names? |