如何在另一个文件中使用类中的变量?

时间:2016-11-17 13:41:05

标签: python-3.x

我有2个脚本,1个保存日志文件,另一个脚本保存电子邮件配置。我需要将日志文件创建脚本中的变量传递给发送电子邮件脚本,我已将日志文件导入发送电子邮件脚本from LogFile import LogFileT

日志代码:

import configparser
from configparser import RawConfigParser
import codecs
import unittest
import time
import sys
import logging


config = configparser.RawConfigParser()
configFilePath = 'C:\\Python\\PI_WebUITesting\\EnvConfig.cfg'
config.read(configFilePath)

class LogFileT:
    def log_file(log_name):
        timestr = time.strftime("%Y%m%d-%H%M%S")
        timestemp = time.strftime("%a, %d %b %Y %H:%M:%S")
        logfilename = config.get('EnvConfig', 'logfile_path') + timestr + log_name
        logging.basicConfig(filename=logfilename, filemode='w',format= timestemp + '--%(levelname)s:%(message)s', level=logging.DEBUG)

SendEmail代码:

import configparser
from configparser import RawConfigParser
import codecs
import unittest
import time
import sys
import logging
import trace
import smtplib,ssl
import base64
from email import encoders
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import formatdate
from LogFile import LogFileT

config = configparser.RawConfigParser()
configFilePath = 'C:\\Python\\PI_WebUITesting\\EnvConfig.cfg'
config.read(configFilePath)

class SendEmail:        
    def send_mail(self,email_subject):
        msg = MIMEMultipart()
        send_from = (config.get('MailConfig', 'send_from'))
        send_to = (config.get('MailConfig', 'send_to'))
        email_filename = LogFile.logfilename
        msg['From'] = send_from
        msg['To'] = send_to
        msg['Date'] = formatdate(localtime = True)
        msg['Subject'] = email_subject
        msg.attach(MIMEText(email_filename))
        part = MIMEBase('application', "octet-stream")
        part.set_payload(open(email_filename, "rb").read())
        encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment; filename="PI_Test.log"')
        msg.attach(part)
        smtp = smtplib.SMTP(config.get('MailConfig', 'smtp'))
        smtp.sendmail(send_from, send_to, msg.as_string())
        smtp.quit()

我需要Log类中的logfilename将其添加到发送电子邮件代码的email_filename

有人可以协助吗?

0 个答案:

没有答案