So I have a python script that is copying a bunch of files between folders. 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. 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. Running manually through python also works, double clicking the python script work. It's only through the scheduler that I can't get it to run. Suffice to say it is quite frustrating. Any ideas?
Asked
Active
Viewed 4,399 times
0
-
1Are you using the full path to the folder? – NightShadeQueen Sep 01 '15 at 16:15
-
1http://gis.stackexchange.com/questions/140110/running-python-script-in-task-scheduler-script-will-not-run – Joe Young Sep 01 '15 at 16:17
-
Perhaps this [answer](http://stackoverflow.com/a/4209102/355230) offers a clue. – martineau Sep 01 '15 at 16:18
-
1the user you setting into the task scheduler have the permission to read and execute the batch file? Try to do a command the append the error to a file: ex: C:/path/to/batch/yourBatch.bat 2>C:/path/to/log – vathek Sep 01 '15 at 16:26
1 Answers
1
When creating the task using Task Scheduler, make sure you give full PATHs to everything, for example:
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__)))

Martin Evans
- 45,791
- 17
- 81
- 97
-
I tried using full paths wherever possible and still not getting the results. This line of python code os.chdir(os.path.dirname(os.path.abspath(__file__))) do I need to sub in the filename and path to the script in there somewhere? – Matt B Sep 01 '15 at 18:20
-
No `__file__` is pre filled by Python when a script is run. It is not defined if you try it from the command line though. – Martin Evans Sep 01 '15 at 19:07
-
1As it turns out, the reason it wasn't running is because it was copying files to another server, and I was using the mapped drive letter in the python script instead of the network address of the server. I switched to the network address in the python script, and now it's running fine on the scheduler. – Matt B Sep 01 '15 at 19:17
-