0

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?

4
  • 1
    Are you using the full path to the folder? Commented Sep 1, 2015 at 16:15
  • 1
    gis.stackexchange.com/questions/140110/… Commented Sep 1, 2015 at 16:17
  • Perhaps this answer offers a clue. Commented Sep 1, 2015 at 16:18
  • 1
    the 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 Commented Sep 1, 2015 at 16:26

1 Answer 1

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__)))
Sign up to request clarification or add additional context in comments.

4 Comments

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?
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.
As 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.
Well spotted, glad you figured it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.