Python 2.7 如何将所有打印重定向到电子邮件正文
我试着把指纹输出到文本文件中。我得到了正确的文本文件输出。邮件发送也可以,但无法将所有打印文本添加到电子邮件正文。首先,将所有打印内容重定向到电子邮件正文。然后想到读取输出电子邮件正文的文本文件,这两种方法都失败了Python 2.7 如何将所有打印重定向到电子邮件正文,python-2.7,Python 2.7,我试着把指纹输出到文本文件中。我得到了正确的文本文件输出。邮件发送也可以,但无法将所有打印文本添加到电子邮件正文。首先,将所有打印内容重定向到电子邮件正文。然后想到读取输出电子邮件正文的文本文件,这两种方法都失败了 import sys, os, arcpy, time import string, smtplib from arcpy import env env.overwriteOutput = True postFix = time.strftime("%Y%m%d") LogsLo
import sys, os, arcpy, time
import string, smtplib
from arcpy import env
env.overwriteOutput = True
postFix = time.strftime("%Y%m%d")
LogsLocation = os.getcwd()
Script = sys.argv[0] # This get the script name
ScriptFile = Script.split('\\')[-1]
LogFileName = ScriptFile.split('.')[0]
def listFcsInGDB():
# set your arcpy.env.workspace to a gdb before calling
for fds in arcpy.ListDatasets('','feature') + ['']:
for fc in arcpy.ListFeatureClasses('','',fds):
for Owner in OwnerList:
if fc.split('.')[-2] == Owner:
yield os.path.join(fds, fc)
def SendEmail(message,Priority):
SUBJECT = "Asset length report for Water"
TO = email
FROM = "AssetLength.Report@DB.production.com"
MIMEversion = "1.0"
ContentType = "text/html"
BODY = string.join((
"From: %s" % FROM,
"To: %s" % TO,
"Subject: %s" % SUBJECT ,
"X-Priority: %s" % Priority ,
"MIME-Version: %s" % MIMEversion,
"Content-type: %s" % ContentType,
"",
message
), "\r\n")
server = smtplib.SMTP('smtpmail',25) #server = smtplib.SMTP(HOST)
server.sendmail(FROM, [TO], BODY)
server.quit()
DEBUG = 1
if DEBUG:
print '\nGenerating asset length report for -\n Effluent, \n Sewer and \n Water ... ...'
Environment = DEV
Email = 'email'
txt = open(LogsLocation + '\\' + postFix + '_' + LogFileName + '_' + Environment + ".txt", 'w')
sys.stdout = txt
print "----------------------" # to email body
print Script # to email body
print "----------------------" # to email body
OwnerList = ['EFFLUENT', 'SEWER']
if Environment == 'DEV':
Server = 'DBRZSQL07'
print "Input: ", Environment # to email body
DatabaseConnectionDirectory = r'\\xxx\ConnectionFiles'
Workspace = env.workspace = DatabaseConnectionDirectory+ '\\' + Server + '\\' + 'Me.sde'
#print "\nWorkspace: ", Workspace, "\n"
print 'Effluent Network' # to email body
print '----------------' # to email body
fcs = listFcsInGDB()
for fc in fcs:
if fc.split('.')[-2]+'.'+fc.split('.')[-1] == 'EFFLUENT.Main':
arcpy.MakeFeatureLayer_management (fc, "EMlyr")
arcpy.SelectLayerByAttribute_management ("EMlyr", "NEW_SELECTION", " OWNER <> 'OTHER' AND LIFECYCLESTATUS <> 'ABANDONED' ")
Length=[]
rows = arcpy.SearchCursor("EMlyr")
for row in rows:
Segment = row.getValue("SHAPE.STLength()")
Length.append(Segment)
total = sum(Length)
#totalkm = sum(Length)/1000
print 'Effluent Main .. ', (round(total, 2)), 'meter' # to email body
arcpy.Delete_management("EMlyr")
print '\nSewer Network' # to email body
print '--------------' # to email body
fcs = listFcsInGDB()
for fc in fcs:
if fc.split('.')[-1] == 'GravityMain':
arcpy.MakeFeatureLayer_management (fc, "GMlyr")
arcpy.SelectLayerByAttribute_management ("GMlyr", "NEW_SELECTION", " OWNER <> 'OTHER' AND LIFECYCLESTATUS <> 'ABANDONED' ")
Length=[]
rows = arcpy.SearchCursor("GMlyr")
for row in rows:
Segment = row.getValue("SHAPE.STLength()")
Length.append(Segment)
total = sum(Length)
#totalkm = sum(Length)/1000
print 'Sewer GravityMain .. ', (round(total, 2)), 'meter' # to email body
arcpy.Delete_management("GMlyr")
print '\n***************************************************\n' # to email body
message = message() # ??
Priority = 3
SendEmail(message,Priority)
else: # Bad arguments
msg = "\nERROR>> Bad arguments."
print msg
导入系统、操作系统、arcpy、时间
导入字符串,smtplib
从arcpy导入环境
env.overwriteOutput=True
后缀=time.strftime(“%Y%m%d”)
LogsLocation=os.getcwd()
Script=sys.argv[0]#这将获取脚本名称
ScriptFile=Script.split('\\')[-1]
LogFileName=ScriptFile.split('.')[0]
def listFcsInGDB():
#在调用之前,将您的arcpy.env.workspace设置为gdb
对于arcpy.ListDataSet(“”,'feature')+[“”]中的FD:
对于arcpy.ListFeatureClass(“”,,,fds)中的fc:
对于所有者列表中的所有者:
如果fc.split('.')[-2]==所有者:
生成os.path.join(fds、fc)
def SendEmail(消息、优先级):
主题=“水资源资产长度报告”
收件人=电子邮件
FROM=“AssetLength。Report@DB.production.com"
MIMEversion=“1.0”
ContentType=“text/html”
BODY=string.join((
“发件人:%s”%From,
“收件人:%s”%To,
“主题:%s”%Subject,
“X优先级:%s”%Priority,
MIME版本:%s”%MIME版本,
“内容类型:%s”%ContentType,
"",
消息
),“\r\n”)
server=smtplib.SMTP('smtpmail',25)#server=smtplib.SMTP(主机)
server.sendmail(发件人,[收件人],正文)
server.quit()
调试=1
如果调试:
打印“\n污水、下水道和水的生成资产长度报告…”
环境=开发
电子邮件='Email'
txt=open(LogsLocation+'\\'+postFix+'\'+LogFileName+'\'+Environment+.txt',w')
sys.stdout=txt
将“-------------------------”打印到电子邮件正文
将脚本打印到电子邮件正文
将“-------------------------”打印到电子邮件正文
OwnerList=['污水','下水道']
如果环境=='DEV':
服务器='DBRZSQL07'
将“输入:”,环境#打印到电子邮件正文
DatabaseConnectionDirectory=r'\\xxx\ConnectionFiles'
Workspace=env.Workspace=DatabaseConnectionDirectory+'\\'+Server+'\\'+'Me.sde'
#打印“\n工作空间:”,工作区“\n”
将“污水网络”打印到电子邮件正文
将“-----------------------”打印到电子邮件正文
fcs=listFcsInGDB()
对于fcs中的fc:
如果fc.split('.')[-2]+'.+fc.split('.')[-1]=='exportent.Main':
arcpy.MakeFeatureLayer_管理(fc,“EMlyr”)
arcpy.SelectLayerByAttribute_管理(“EMlyr”、“新_选择”、“所有者‘其他’和LIFECYCLESTATUS‘放弃’”)
长度=[]
行=arcpy.SearchCursor(“EMlyr”)
对于行中的行:
段=行.getValue(“SHAPE.STLength()”)
追加长度(段)
总计=总和(长度)
#总公里数=总和(长度)/1000
打印“污水干管…”,(四舍五入(共2个)),“米”到电子邮件正文
arcpy.删除_管理(“EMlyr”)
将“\n电源网络”打印到电子邮件正文
将“--------------”打印到电子邮件正文
fcs=listFcsInGDB()
对于fcs中的fc:
如果fc.split('.')[-1]=='GravityMain':
arcpy.MakeFeatureLayer_管理(fc,“GMlyr”)
arcpy.SelectLayerByAttribute_管理(“GMlyr”、“新_选择”、“所有者‘其他’和LIFECYCLESTATUS‘废弃’”)
长度=[]
行=arcpy.SearchCursor(“GMlyr”)
对于行中的行:
段=行.getValue(“SHAPE.STLength()”)
追加长度(段)
总计=总和(长度)
#总公里数=总和(长度)/1000
打印“下水道重力主…”,(四舍五入(共2个)),“米”到电子邮件正文
arcpy.删除_管理(“GMlyr”)
将“\n***************************************************************************\n”打印到电子邮件正文
消息=消息()#??
优先级=3
发送电子邮件(消息、优先级)
其他:#错误的论点
msg=“\n错误>>错误参数。”
打印味精
您需要将消息文本作为字符串传递到函数sendmail()
。因此,您需要收集希望显示在消息正文中的所有行,而不是打印语句
而不是
print "----------------------" # to email body
print Script # to email body
print "----------------------" # to email body
print 'Sewer GravityMain .. ', (round(total, 2)), 'meter' # to email body
这样做
payload = []
...
payload.append("----------------------")
payload.append(Script)
payload.append("----------------------")
如果print
语句中有多个项,则需要将它们组合成一个格式化字符串。所以不是
print "----------------------" # to email body
print Script # to email body
print "----------------------" # to email body
print 'Sewer GravityMain .. ', (round(total, 2)), 'meter' # to email body
做
列表payload
是一个字符串列表。发送之前,从该列表中创建一个字符串,元素之间使用换行符
message = '\n'.join(payload)
SendEmail(message,Priority)
您需要将消息文本作为字符串传递到函数
sendmail()
。因此,您需要收集希望显示在消息正文中的所有行,而不是打印语句
而不是
print "----------------------" # to email body
print Script # to email body
print "----------------------" # to email body
print 'Sewer GravityMain .. ', (round(total, 2)), 'meter' # to email body
这样做
payload = []
...
payload.append("----------------------")
payload.append(Script)
payload.append("----------------------")
如果print
语句中有多个项,则需要将它们组合成一个格式化字符串。所以不是
print "----------------------" # to email body
print Script # to email body
print "----------------------" # to email body
print 'Sewer GravityMain .. ', (round(total, 2)), 'meter' # to email body
做
列表payload
是一个字符串列表。发送之前,从该列表中创建一个字符串,元素之间使用换行符
message = '\n'.join(payload)
SendEmail(message,Priority)
杰出的我基本上是在找这个。我还使用了“
”而不是“\n”\当“
”执行此任务时,n”不会打断电子邮件中的行。您没有说您将电子邮件显示为HTML。对不起,野猪。我以为所有的电子邮件都是html格式的。别理我,没问题。不管怎么说,你都解决了,这是最重要的。太好了。我基本上是在找这个。我还使用了“
”而不是“\n”\当“
”执行此任务时,n”不会打断电子邮件中的行。您没有说您将电子邮件显示为HTML。对不起,野猪。我以为所有的电子邮件都是html格式的。别理我,没问题。不管怎么说,你都解决了,而且