2017-11-26 2 views
0

IMAP 서버에 다음 사서함이 있습니다 (첨부 된 스크린 샷 참조). enter image description hereIMAP 서버에서 특정 사서함을 선택하는 방법은 무엇입니까?

사서함 Folder1 만 선택하고 하위 디렉터리가 있는지 확인하고 싶습니다.

svr = imaplib.IMAP4_SSL(imap_address) 
svr.login(user, pwd) 
svr.select('inbox') <<<<<<<<<<<<<<<<< 
rv, data = svr.search(None, "ALL") 
test, folders = svr.list('""', '*') 
print(folders) 

내가 폴더 1을 선택한 것하고 내가 하위 디렉토리를 검색 할 수있는 '폴더 1'(화살표로 표시 문)에 '받은 편지함'을 변경하는 생각 : 나는 이미 다음 코드를 시도했다. 그러나 아무 일도 일어나지 않았으며 여전히 '받은 편지함'과 동일한 결과를 보여줍니다.

누군가 내가 여기서 잘못하고있는 것을 이해할 수 있습니까?

+0

LIST는 현재 선택된 사서함에서 작동하지 않으며 전역 명령입니다. list()의 매개 변수를 변경해 보셨습니까? – Max

+0

@Max 나는 그것에 대해 연구했고 원하는 결과를 얻을 수있었습니다. 코드를 솔루션으로 게시합니다. 다른 것을 수정해야하거나 프로세스를 더 빠르게 만들 수 있는지 확인하십시오. –

답변

0

나는 폴더의 이름을 몰랐기 때문에 다른 접근 방식을 시도했다. 먼저 루트 디렉토리의 모든 폴더를 수집 한 다음 하위 디렉토리가 있는지 확인하기 위해 하나씩 구문 분석합니다.

root_folders = [] 
svr = imaplib.IMAP4_SSL(imap_address) 
svr.login(user, pwd) 
svr.select('inbox') 
response, folders = svr.list('""', '*') 

def parse_mailbox(data): 
    flags, b, c = data.partition(' ') 
    separator, b, name = c.partition(' ') 
    return flags, separator.replace('"', ''), name.replace('"', '') 

def subdirectory(folder): 
    #For directories 'Deleted Items', 'Sent Items', etc. with whitespaces, 
    #the name of the directory needs to be passed with double quotes, hence '"' + name + '"' 
    test, folders = obj.list('""','"' + name+ '/*"') 
    if(folders is not None): 
     print('Subdirectory exists') # you can also call parse_mailbox to find the name of sub-directory 

for mbox in folders: 
    flags, separator, name = parse_mailbox(bytes.decode(mbox)) 
    fmt = '{0} : [Flags = {1}; Separator = {2}' 
    if len(name.split('/')) > 1: 
     continue 
    else: 
     root_folders.append(name) 

for folder in root_folders: 
    subdirectory(folder) 

내 스크립트의 맞춤 코드이지만이 질문에 대한 해결책이되어야합니다.

관련 문제