Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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脚本捕获终端输出到文本文件_Python - Fatal编程技术网

如何从我的python脚本捕获终端输出到文本文件

如何从我的python脚本捕获终端输出到文本文件,python,Python,我想在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,

我想在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
导入base64
导入sendgrid
导入操作系统
作为pd进口熊猫
导入时间
从日期时间导入日期,时间增量
从sendgrid.helpers.mail导入电子邮件、内容、邮件、附件
#Python 3
将urllib.request导入为urllib
除恐怖外:
#Python 2
将urllib2导入为urllib
list=pd.read\u csv('emaildb.csv')
电子邮件=列表['email']
商户=列表[“商户”]
今天=日期。今天()
yest=今天-时间增量(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
而我
$python script\u name.py>logs.txt 2>errors.txt
如果要在文本文件中包含日期和时间,该怎么办?是否要重定向python文件中的某些语句?或者将脚本中打印出来的内容重定向到文件中@Cel@DeveshKumarSingh我想打印到.txt文件,不管终端中的输出消息是什么,顺便说一句,我通过.bat调用python脚本,并计划通过windows任务调度器运行它。然后使用记录器模块,在该模块中,您可以从文件中定向任何您想要的内容
$python script_name.py>logs.txt 2>errors.txt
如果要在文本文件中包含日期和时间,该怎么办?是否要重定向python文件中的某些语句?或者将脚本中打印出来的内容重定向到文件中@Cel@DeveshKumarSingh我想打印到.txt文件,不管终端中的输出消息是什么,顺便说一句,我通过.bat调用python脚本,并计划通过windows任务调度器运行它。然后使用记录器模块,在该模块中,您可以从文件中定向任何您想要的内容!