首页 > 解决方案 > 无法对包含 .py 文件的批处理文件进行任务调度以在 Windows 10 中启动

问题描述

我有以下批处理文件

SET logfile="C:\Reports\logbatch.log"
@echo off
@echo Starting Script at %date% %time% >> %logfile%
"C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\python.exe" 
"C:\Users\mypc\source\repos\WebS\WebS\Start_Master.py"
@echo finished at %date% %time% >> %logfile%
pause

Start_Master.py包含与 Start_Master.py 位于同一文件夹中的其他几个脚本

如果我手动运行批处理文件,它可以工作。

当我创建任务并且任务自动或手动运行时,我得到它无法打开文件

python: can't open the file 'script1.py' No such file or directory
python: can't open the file 'script2.py' No such file or directory
python: can't open the file 'script3.py' No such file or directory

标签: python

解决方案


尝试:

SET logfile="C:\Reports\logbatch.log"
@echo off
@echo Starting Script at %date% %time% >> %logfile%
cd "C:\Users\mypc\source\repos\WebS\WebS\"
"C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\python.exe" 
"C:\Users\mypc\source\repos\WebS\WebS\Start_Master.py"
@echo finished at %date% %time% >> %logfile%
pause

尝试将CWD(当前工作目录)更改为Start_Master.py所在的目录


推荐阅读