2017-11-10 17 views
0
for file in os.listdir(path): 
try: 
    with open(file, 'r') as fp: 
     msg = MIMEBase('application', "octet-stream") 
     msg.set_payload(fp.read()) 
    encoders.encode_base64(msg) 
    msg.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file)) 
    outer.attach(msg) 
except: 
    print("Unable to open one of the attachments. Error: ", sys.exc_info()[0]) 
    raise 

composed = outer.as_string() 

"FileNotFoundError : [Errno 2] No such file or directory"가 표시되지만 파일이 존재합니다! os.listdir() 객체 유형이 될 수 있습니까?os.listdir (경로)를 사용할 때 파일을 열 수 없습니다.

답변

0

os.listdir 함수는 파일 이름 만 반환합니다. 경로와 파일 이름을 연결해야합니다.

for file in os.listdir(path): 
    try: 
     with open(ps.path.join(path, file), 'r') as fp: 
    ...