我想在运行python脚本时捕获终端消息并将其保存到文本文件中。我也想捕获错误消息。
-不想使用批处理文件执行此操作。 -帮我把这些命令放在我的脚本上
import base64
import sendgrid
import os
import pandas as pd
import time
from datetime import date, timedelta
from sendgrid.helpers.mail import Email, Content, Mail, Attachment
# Python 3
import urllib.request as urllib
except ImportError:
# Python 2
import urllib2 as urllib
list = pd.read_csv('emaildb.csv')
email = list['Email']
merchant = list['Merchant']
today = date.today()
yest = today - timedelta(1)
yest1 = yest.strftime('%Y%m%d')
yest2 = yest.strftime('%Y-%m-%d')
now1 = time.strftime("%B %d %Y %H:%M:%S")
sg = sendgrid.SendGridAPIClient(apikey='sample API')
i = 0
while i < len(list):
file_name1 = "REPORT_"+yest1+"_"+str(merchant[i])+".pdf"
file_name2 = "REPORT_"+yest1+"_"+str(merchant[i])+".xls"
S1="REPORT "+str(merchant[i])+" "+str(yest1)
B1 = "Test"
with open(file_name1,'rb') as f:
data1 = f.read()
encoded1 = base64.b64encode(data1).decode()
with open(file_name2,'rb') as f:
data2 = f.read()
encoded2 = base64.b64encode(data2).decode()
mail = {
"attachments": [
{
"content": encoded1,
"filename": file_name1,
},
{
"content": encoded2,
"filename": file_name2,
}
],
"personalizations": [
{
"to": [{
"email": email[i]
}],
"subject": S1
}
],
"from": {
"email": "test.com"
},
"content": [
{
"type": "text/html",
"value": B1
}
]
}
response = sg.client.mail.send.post(request_body=mail)
print("["+now1+"] "+email[i]+" "+file_name1)
if response.status_code == 202:
print('Success')
else: print('Failed')
print("\n")
i = i + 1