2016-07-29 1 views
1

특정 파일이 생성 될 때 자동으로 이메일을 전송하는 python 스크립트를 작성하려고합니다. 전자 메일을 보내는 코드가 있지만 특정 파일을 찾는 디렉터리를 모니터링하는 방법을 모르겠다 고 생각합니다.은 파일이 생성 될 때 자동으로 이메일을 보냅니다.

하이 레벨의 예는 다음 디렉토리 foo를 이내에서

/ 파일 바즈가 채워지면 your_file.txt가 생성되었을 때 모니터 sendEmail()

+0

어떤 운영체제에 있습니까? – Chris

+0

@Chris on windows – StillLearningToCode

답변

0
import os.path 
if os.path.exists(file_path) and os.path.isfile(fname): 
    send_email() 
0
import smtplib,time 

기능을 수행

def search_file(): 
    try: 
     my_file=open('your_file.txt','r') 
     content=my_file.read() 
     my_file.close() 
     return content 
    except: 
     print "Waiting..." 
     time.sleep(20) 
     search_log()                          

서버 및 포트

,451,515,
mail=smtplib.SMTP('smtp.gmail.com',587) 

파일이 위치한 경로/디렉토리의 독립적 인 로그인

mail.starttls() 
mail.login('user','pwd') 



mail.sendmail('originator','receiver',search_log()) 

mail.close() 
0

또 다른 방법에 대해 = 서버 ESMTP

mail.ehlo() 

시작 TLS 모드로 암호화 SMTP를 식별 :

import os 
if 'your_file.txt' in os.listdir(os.getcwd()): 
#do something 

관련 문제