2016-07-29 94 views
1

我正在嘗試編寫一個python腳本,該腳本在生成特定文件時自動發送電子郵件。我想我有發送電子郵件的代碼,但我不知道如何監視尋找特定文件的目錄。當文件生成時自動發送電子郵件

高電平的例子是:

從目錄foo中/ 當文件巴茲填充做sendEmail()

+0

在哪個操作系統上? – Chris

+0

@Chris在windows上 – StillLearningToCode

回答

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

功能時生成your_file.txt其監視

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()                          

服務器和端口

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

識別到服務器ESMTP

mail.ehlo() 

開始TLS模式=加密SMTP進行登錄

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



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

mail.close() 
0

的另一種方式,這是獨立的路徑/目錄,其中所述文件位於的:

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