2016-07-14 30 views
0

Python新手和我唯一的「編碼」經驗是SQL,因此請和我一起袒護。從For循環發送多個文件路徑的電子郵件

我有一個for循環,將數據從共享位置的電子表格(xlsx文件)插入到數據庫表中。運行完成後,我想將處理過的文件名稱的電子郵件發送到通訊組列表。現在我知道如何在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

@JohnGordon的確我做了,相應地修改! –