Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:应为字符串有效负载:<;类别';字节'&燃气轮机;_Python_Python 3.x - Fatal编程技术网

Python TypeError:应为字符串有效负载:<;类别';字节'&燃气轮机;

Python TypeError:应为字符串有效负载:<;类别';字节'&燃气轮机;,python,python-3.x,Python,Python 3.x,我正在使用Python 3.2。 我使用以下代码编写了一个构建日志文件: rsltFile = open('buildLog.txt', 'wb') p = subprocess.Popen('call ant compile', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) for line in p.stdout.readlines(): rsltFile.write(line)

我正在使用Python 3.2。 我使用以下代码编写了一个构建日志文件:

rsltFile = open('buildLog.txt', 'wb')
    p = subprocess.Popen('call ant compile', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    for line in p.stdout.readlines(): 
        rsltFile.write(line) 
    retval = p.wait()
然后,我使用以下命令发送一封电子邮件,并附上上述文件:

def send_mail(send_from, send_to, subject, text, files=[], server="XXX.XXX.com"):
  assert type(send_to)==list
  assert type(files)==list

  msg = MIMEMultipart()
  msg['From'] = send_from
  msg['To'] = COMMASPACE.join(send_to)
  msg['Date'] = formatdate(localtime=True)
  msg['Subject'] = subject

  msg.attach( MIMEText(text) )

  for f in files:
    part = MIMEBase('application', "octet-stream")
    part.set_payload( open(f,"rb").read() )
    part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
    msg.attach(part)

  smtp = smtplib.SMTP(server)
  smtp.sendmail(send_from, send_to, msg.as_string())
  smtp.close()
  File "C:\workspace\VCT2400_Service\ServiceApplication\autobuild\myMail.py", line 29, in send_mail
    smtp.sendmail(send_from, send_to, msg.as_string())
  File "C:\Python32\lib\email\message.py", line 168, in as_string
    g.flatten(self, unixfrom=unixfrom)
  File "C:\Python32\lib\email\generator.py", line 91, in flatten
    self._write(msg)
  File "C:\Python32\lib\email\generator.py", line 137, in _write
    self._dispatch(msg)
  File "C:\Python32\lib\email\generator.py", line 163, in _dispatch
    meth(msg)
  File "C:\Python32\lib\email\generator.py", line 224, in _handle_multipart
    g.flatten(part, unixfrom=False, linesep=self._NL)
  File "C:\Python32\lib\email\generator.py", line 91, in flatten
    self._write(msg)
  File "C:\Python32\lib\email\generator.py", line 137, in _write
    self._dispatch(msg)
  File "C:\Python32\lib\email\generator.py", line 163, in _dispatch
    meth(msg)
  File "C:\Python32\lib\email\generator.py", line 192, in _handle_text
    raise TypeError('string payload expected: %s' % type(payload))
TypeError: string payload expected: <class 'bytes'>
但是我得到了以下错误:

def send_mail(send_from, send_to, subject, text, files=[], server="XXX.XXX.com"):
  assert type(send_to)==list
  assert type(files)==list

  msg = MIMEMultipart()
  msg['From'] = send_from
  msg['To'] = COMMASPACE.join(send_to)
  msg['Date'] = formatdate(localtime=True)
  msg['Subject'] = subject

  msg.attach( MIMEText(text) )

  for f in files:
    part = MIMEBase('application', "octet-stream")
    part.set_payload( open(f,"rb").read() )
    part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
    msg.attach(part)

  smtp = smtplib.SMTP(server)
  smtp.sendmail(send_from, send_to, msg.as_string())
  smtp.close()
  File "C:\workspace\VCT2400_Service\ServiceApplication\autobuild\myMail.py", line 29, in send_mail
    smtp.sendmail(send_from, send_to, msg.as_string())
  File "C:\Python32\lib\email\message.py", line 168, in as_string
    g.flatten(self, unixfrom=unixfrom)
  File "C:\Python32\lib\email\generator.py", line 91, in flatten
    self._write(msg)
  File "C:\Python32\lib\email\generator.py", line 137, in _write
    self._dispatch(msg)
  File "C:\Python32\lib\email\generator.py", line 163, in _dispatch
    meth(msg)
  File "C:\Python32\lib\email\generator.py", line 224, in _handle_multipart
    g.flatten(part, unixfrom=False, linesep=self._NL)
  File "C:\Python32\lib\email\generator.py", line 91, in flatten
    self._write(msg)
  File "C:\Python32\lib\email\generator.py", line 137, in _write
    self._dispatch(msg)
  File "C:\Python32\lib\email\generator.py", line 163, in _dispatch
    meth(msg)
  File "C:\Python32\lib\email\generator.py", line 192, in _handle_text
    raise TypeError('string payload expected: %s' % type(payload))
TypeError: string payload expected: <class 'bytes'>
文件“C:\workspace\VCT2400\u Service\ServiceApplication\autobuild\myMail.py”,第29行,在send\u mail中
smtp.sendmail(发送发件人、发送收件人、msg.as\u字符串())
文件“C:\Python32\lib\email\message.py”,第168行,格式为字符串
g、 展平(自,unixfrom=unixfrom)
文件“C:\Python32\lib\email\generator.py”,第91行,扁平化
自我书写(msg)
文件“C:\Python32\lib\email\generator.py”,第137行,以书面形式
自动发送(msg)
文件“C:\Python32\lib\email\generator.py”,第163行,在
甲基(味精)
文件“C:\Python32\lib\email\generator.py”,第224行,在多部分中
g、 展平(零件,unixfrom=False,linesep=self.\u NL)
文件“C:\Python32\lib\email\generator.py”,第91行,扁平化
自我书写(msg)
文件“C:\Python32\lib\email\generator.py”,第137行,以书面形式
自动发送(msg)
文件“C:\Python32\lib\email\generator.py”,第163行,在
甲基(味精)
文件“C:\Python32\lib\email\generator.py”,第192行,文本
raise TypeError('应为字符串负载:%s'%type(负载))
TypeError:应为字符串有效负载:
file.read(在“rb”模式下打开)在Python 3中返回字节,您需要在“rt”模式下打开文件或使用将字节解码为字符串

我强烈建议观看

这与以下内容密切相关: