(Python) The scheduled task program with open cannot write to the log file

Situation background

项目需要 , 在服务器上为程序记录日志 ,在命令行可以实现 , 
但是在任务计划程序内部无法实现 , 加了权限也没用的情况下 ,
原因就在于我们没有设置好“操作”,我们只设置了“程序或脚本”,但是没有设置“起始于(可选)”选项,因为这个是可选的,所以很多人认为这个选项是多余的
,是的,如果只需要执行.py文件,如果不需要读写操作是不需要设置,但是如果程序在 Windows 计划任务中要**读写**文件就一定要设置“起始于(可选)”这个选项,这个选项就是设置.exe程序的文件根目录。如下面的设置

Insert picture description here
By default, cywin's command execution points to C:/tool/program/. I entered this directory and found the log file I lost. I was happy to copy it out to the real running directory.
In addition, the content that needs to be noted is: , If the absolute path is used, the path of file storage has nothing to do with the execution path,

import time
 
localtime = time.asctime(time.localtime(time.time()))
print(localtime)
while True:
	with open('C:/projects/p1/testr_log/22.txt','a') as f:
		f.write(localtime+'hello python'+'\n')

https://www.fengxiaochuang.com/?p=156

Guess you like

Origin blog.csdn.net/weixin_42540340/article/details/108599373