Python将记录插入到另一个批处理作业使用的日志文件中

问题描述 投票:0回答:1

我正面临一个特殊的问题。我有一个批处理脚本,该脚本使用名为logfile.txt的日志文件。本质上,此批处理作业不过是一系列xcopy和python文件,其运行方式如下:

xcopy /I "source" "destination /D" >> D:\logfile\logfile.txt
xcopy /I "source" "destination /D" >> D:\logfile\logfile.txt
....
cd C:\ProgramData\Anaconda3
pyhton D:\python_files\py_file_1.py >> D:\logfile\logfile.txt
python D:\python_files\py_file_2.py >> D:\logfile\logfile.txt
....

现在我想提两件事。我的问题符合这两点。

  1. 虽然我可以看到xcopy相关内容正在日志文件中显示(即已复制的文件数),没有运行的日志条目python工作。为什么这样?
  2. 我还有另一个python脚本(如下所示),该脚本会填充另一个名为py_log.txt的.txt文件,以防在运行上述py文件时发生任何错误。如果没有错误,则插入标准行。我想将这些错误条目或标准条目插入批次logfile.txt

    lines = []
    with open('py_log.txt','r') as f_in:
         z = str(f_in)
         lines.append(z)
    with open('logfile.txt', 'a') as f_out:    
         if len(lines) > 1:
             f_out.write('\n'+lines) # Error Entry if Any
         else:
             f_out.write('\n'+'Data load job has been completed at {0}'.format(datetime.datetime.now().strftime('%Y-%m-%d %H-%M')
    

问题是,当我在Task Scheduler上安排上述python脚本时,它没有在logfile.txt中插入任何文本。当我独立运行此程序时,它工作正常。

为什么将上面的脚本放在Task Scheduler上时无法写。我想念什么?

python batch-file
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.