You are not following PEP8, Python's official coding style guide. A couple of violations that jump in the eye:
- Don't put multiple imports on the same line
- Put a space after commas in parameter lists, so
m.login(user, pwd)instead ofm.login(user,pwd) - There shouldn't be whitespace after
: - Use at least 2 spaces before inline comments (before each
# comment) - Put a space after
#when starting comments, so# commentinstead of#comment - Some lines are too long (longer than 79 characters)
Some other bad practices:
- Unused imports: getpass, csv, threading
- No need for the parens in
user = ("username"), and it may get confused as a tuple (which it isn't) - At the point where you do
str(message),messagemight not have been assigned yet - At the point where you do
os.listdir(path),pathmight not have been assigned yet
In this code:
resp, items = m.search(None, "ALL") items = items[0].split() # getting the mails id for emailid in items: resp, data = m.fetch(emailid, "(RFC822)")
The initialization of resp is pointless, because it will be reassigned anyway in the loop. Effectively you throw away the first assignment of resp. In such cases a common practice is to use _ as the variable name, like this:
_, items = m.search(None, "ALL")