IMAP Checking Folder Size

Discussion in 'Python' started by Kevin F, Mar 20, 2006.

  1. Kevin F

    Kevin F Guest

    I'm trying to use the following code to get my remote server's folder
    size information. Unfortunately, i'm getting the error:



    Traceback (most recent call last):
    File "/Life/School/Homework/Spring 2006/OPIM
    399/Tutorial/IMAP/mailboxsize.py", line 23, in -toplevel-
    number_of_messages_all += int(number_of_messages[0])
    ValueError: invalid literal for int(): The requested item could not be
    found.




    What seems to be the problem?

    My code is here:




    import sys, os, string, imaplib, getpass

    imap_server = "webmail.xxxxx.xxxxx.edu"

    # Open a connection to the IMAP server
    M = imaplib.IMAP4_SSL(imap_server)
    M.login('xxxx', getpass.getpass())

    # The list of all folders
    result,list = M.list()

    print "%-30s%5s%10s\n" % ("Folder", "# Msg", "Size")

    number_of_messages_all = 0
    size_all = 0

    for item in list[:]:
    x = item.split()
    mailbox = string.join(x[2:])

    # Select the desired folder
    result, number_of_messages = M.select(mailbox, readonly=1)
    number_of_messages_all += int(number_of_messages[0])

    size_folder = 0
    # Go through all the messages in the selected folder
    typ, msg = M.search(None, 'ALL')
    # Find the first and last messages
    m = [int(x) for x in msg[0].split()]
    m.sort()
    if m:
    message_set = "%d:%d" % (m[0], m[-1])
    result, sizes_response = M.fetch(message_set, "(UID RFC822.SIZE)")
    for i in range(m[-1]):
    tmp = sizes_response.split()
    size_folder += int(tmp[-1].replace(')', ''))
    else:
    size_folder = 0
    print "%-30s%5d%10s" % (mailbox, int(number_of_messages[0]),
    size_folder);
    size_all += size_folder

    print "\n%-30s%5i%10.3f MB\n" % ("Sum", number_of_messages_all,
    size_all/1e6)

    # Close the connection
    M.logout()
     
    Kevin F, Mar 20, 2006
    #1
    1. Advertising

  2. Kevin F

    Donn Cave Guest

    In article <dvmh9e$ev0o$>,
    Kevin F <> wrote:
    > I'm trying to use the following code to get my remote server's folder
    > size information. Unfortunately, i'm getting the error:
    >
    >
    >
    > Traceback (most recent call last):
    > File "/Life/School/Homework/Spring 2006/OPIM
    > 399/Tutorial/IMAP/mailboxsize.py", line 23, in -toplevel-
    > number_of_messages_all += int(number_of_messages[0])
    > ValueError: invalid literal for int(): The requested item could not be
    > found.


    ....
    > # Select the desired folder
    > result, number_of_messages = M.select(mailbox, readonly=1)
    > number_of_messages_all += int(number_of_messages[0])


    A general observation about imaplib, the caller usually has
    some analysis to do on the returned data. For example, you
    have to check that `result' value, before you can assume that
    you got the data you asked for. I would suggest that you print
    these values out somewhere, it will put you in a position where
    you can probably answer your question better than we can.

    Donn Cave,
     
    Donn Cave, Mar 20, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henrik Ormåsen

    Curier-IMAP and imap.create()

    Henrik Ormåsen, Aug 19, 2006, in forum: Ruby
    Replies:
    0
    Views:
    178
    Henrik Ormåsen
    Aug 19, 2006
  2. Jon Fi
    Replies:
    4
    Views:
    544
    Kashia Buch
    Oct 21, 2006
  3. jasonnaylor
    Replies:
    1
    Views:
    256
    jasonnaylor
    Apr 16, 2008
  4. Adam Akhtar
    Replies:
    1
    Views:
    179
    Eric Hodel
    Dec 15, 2008
  5. Abhishiv Saxena

    Support for IMAP IDLE in net/imap

    Abhishiv Saxena, Jul 3, 2009, in forum: Ruby
    Replies:
    4
    Views:
    197
    Eric Hodel
    Jul 4, 2009
Loading...

Share This Page