简体   繁体   English

Python脚本未在任务计划程序中运行

[英]Python script not running in task scheduler

So I have a python script that is copying a bunch of files between folders. 所以我有一个python脚本正在文件夹之间复制一堆文件。 This script runs exactly as intended until I try to use the task scheduler to get it to run at regular intervals. 在我尝试使用任务计划程序使其定期运行之前,该脚本将按预期运行。 I have run it several different ways and it always runs fine, but when I create a scheduled task it gives me an error (it says it can't find a folder that I have confirmed exists). 我用几种不同的方式运行它,并且它总是可以正常运行,但是当我创建一个计划任务时,它给我一个错误(它说找不到我确认存在的文件夹)。 I've tried creating a batch file to launch the python script through the task scheduler and it doesn't work, even though it works when I run the batch file manually. 我尝试创建一个批处理文件以通过任务计划程序启动python脚本,尽管我手动运行该批处理文件时它可以工作,但它不起作用。 I've also tried calling python through the scheduled task with the python script as an argument through the scheduler and it doesn't work. 我也尝试过通过计划任务使用python脚本作为调度程序的自变量调用python,但它不起作用。 Running manually through python also works, double clicking the python script work. 通过python手动运行也可以,双击python脚本即可。 It's only through the scheduler that I can't get it to run. 仅通过调度程序,我无法运行它。 Suffice to say it is quite frustrating. 可以说这很令人沮丧。 Any ideas? 有任何想法吗?

When creating the task using Task Scheduler, make sure you give full PATHs to everything, for example: 使用任务计划程序创建任务时,请确保为所有内容提供完整的PATH,例如:

Program/script: C:\Python27\python.exe
Add arguments (optional): -u "C:\Users\MyUserName\Documents\MyScript.py"
Start in (optional): C:\Users\MyUserName\Documents

Secondly, you could also force the working folder from within your script: 其次,您还可以从脚本中强制使用工作文件夹:

import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM