2
전자 메일 첨부 파일로 압축해야하는 모든 파일이 들어있는 파일 목록이 있습니다. 파일 목록을 압축하고 전자 메일에 첨부하십시오 - python
현재 내가 사용하고있는 코드는이 날 모든 파일을 zip 파일을 만들 수 있습니다 및 우편을 첨부if zip_file == True:
for file in file_list:
zf = tempfile.TemporaryFile(prefix='mail', suffix='.zip')
zip = zipfile.ZipFile(zf, 'a')
zip.write(file)
zip.close()
zf.seek(0)
part = MIMEBase('application', 'zip')
part.set_payload(zf.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment', filename='xls_attachments' + '.zip')
msg.attach(part)
입니다. 그래서 만약 내가 file_list
에 10 개의 파일을 가지고 있다면 그 메일은 10 개의 압축 된 첨부 파일을 가지게 될 것입니다. 하지만 내가 원하는 것은 10 개의 파일을 모두 하나의 zip 파일에 담는 것입니다.
여기서 각 파일을 작성하기 위해 복수 zip.write()
메서드를 사용하는 예제를 보았습니다. 하지만 file_list
의 항목 수를 제어 할 수 없기 때문에 이는 좋지 않습니다.
이 응답을 봐 : http://stackoverflow.com/questions/25544475/using-python-to- a-zip 파일에 파일 목록 추가 – Humbalan