2016-07-29 3 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()                          

サーバーとポート

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

サーバーESMTP

mail.ehlo() 

開始TLSモード=ログイン

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



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

mail.close() 
0

のための暗号化SMTPにファイルが置かれているパス/ディレクトリとは独立した別の方法を特定:

import os 
if 'your_file.txt' in os.listdir(os.getcwd()): 
#do something 
関連する問題