2016-07-14 1 views
0

Pythonが初めてで、私の唯一の "コーディング"経験はSQLです。Forループからの複数のファイルパスを使用した電子メールの送信

私は、スプレッドシート(​​xlsxファイル)のデータを共有場所にデータベーステーブルに挿入するforループを持っています。実行が完了したら、処理されたファイル名の電子メールを配布リストに送信したいと思います。今私はforループでこれを行う方法を知っていますが、私は単一の電子メールを送信したいだけです。私の主な問題は、ファイル名を格納する方法がわからず、ループが完了した後、すべてのファイル名のリストを電子メールで送信することです。私は配列でこれを行うことを考えていたが、それは非効率的だと思われる。私はStringIO(またはcStringIO)も見ていました。

import xlrd 
import pymssql 
import glob 
import os 
import shutil 
import uuid 
from time import gmtime, strftime 


path = 'c:\\Test\\' 
source = glob.glob(os.path.join(path, '*.xls*')) 
print source 

if not source: 
    exit() 

for sf in source: 


    # Establish a SQL connection 
    database = pymssql.connect("localhost", "username", "password", 
           "testdb") 

    # Get the cursor, which is used to traverse the database, line by line 
    cursor = database.cursor() 

    #Parse name from file 
    name = sf 
    delivery,type,name,date = name.split("_",3) 

    # Create the INSERT INTO sql query 
    query = """INSERT INTO testdb(value1,value2) VALUES (%s, %s)""" 


    # Open the workbook and define the worksheet 
    book = xlrd.open_workbook(sf) 
    sheet = book.sheet_by_name("Test") 
    date = strftime("%Y-%m-%d %H:%M:%S", gmtime()) 
    assetuuid = str(uuid.uuid1()) 

    for r in range(1, sheet.nrows): 
     value1 = sheet.cell(r, 3).value 
     value2 = sheet.cell(r,9).value 
     values = (value1, value2) 
     cursor.execute(query, values) 

    shutil.move(sf, 'c:\\Test\\Archive\\') 

    # Close the cursor 
    cursor.close() 

    # Commit the transaction 
    database.commit() 

# Close the database connection 
database.close() 

ありがとうございます!

+0

あなたはデータを挿入すると、リストオブジェクトにファイル名を追加します。次に、ループが終了すると、ファイル名のリストを電子メール本文にダンプするだけです。 –

+0

ありがとうDavid、私はコードを追加しました。 – HMan06

答えて

2

これは、あなたが使用したファイルキャプチャするために非常に単純です:電子メールを送信するよう

import xlrd 
import pymssql 
import glob 
import os 
import shutil 
import uuid 
from time import gmtime, strftime 


path = 'c:\\Test\\' 
source = glob.glob(os.path.join(path, '*.xls*')) 
print source 

if not source: 
    exit() 

files_used = [] ## this will contain list of file names which we processed 

for sf in source: 


    # Establish a SQL connection 
    database = pymssql.connect("localhost", "username", "password", 
           "testdb") 

    # Get the cursor, which is used to traverse the database, line by line 
    cursor = database.cursor() 

    #Parse name from file 
    name = sf 
    delivery,type,name,date = name.split("_",3) 

    # Create the INSERT INTO sql query 
    query = """INSERT INTO testdb(value1,value2) VALUES (%s, %s)""" 


    # Open the workbook and define the worksheet 
    book = xlrd.open_workbook(sf) 
    sheet = book.sheet_by_name("Test") 
    date = strftime("%Y-%m-%d %H:%M:%S", gmtime()) 
    assetuuid = str(uuid.uuid1()) 
    # Append this filename to our list: 
    files_used.append(sf) 
    for r in range(1, sheet.nrows): 
     value1 = sheet.cell(r, 3).value 
     value2 = sheet.cell(r,9).value 
     values = (value1, value2) 
     cursor.execute(query, values) 

    shutil.move(sf, 'c:\\Test\\Archive\\') 

    # Close the cursor 
    cursor.close() 

    # Commit the transaction 
    database.commit() 

# Close the database connection 
database.close() 

""" 
Now that you're done processing the files, 
you can send your email using the files_used list object 
""" 

を、あなたは電子メールを送信するために既存のコードを持っていますか? (そうでない場合は、いくつかを見つけてそれを実装しようとする - このようなことをするために使用できるサンプルとライブラリがたくさんあるはずです)。

電子メールがリストfiles_usedを取るべきであると単純にリストの要素を結合して電子メールの本文にリスト値を追加することができます。

""" 
Assumes you have some name like 'email_body' which 
you are using to construct the email contents, then 
at some point you want to insert the list of 'files_used' 
""" 

email_body += '\n'.join(files_used) 
+1

あなたは '/ n'ではなく' \ n'を意味すると思います。 –

+0

@ジョンゴードン確かに私は、それに応じて改訂しました! –

関連する問題