Home > database >  Batch file with python script gives an error
Batch file with python script gives an error

Time:05-27

I have a python script in a batch file with the following code. This runs fine when I double click the .bat file to run it. However, when I run it through Task Scheduler it gives a 0x1 run error. I've tried experimenting and it seems removing the >log.txt 2>&1 allows it to run from task scheduler. I added this line to log any exceptions and print statements to a txt file. Does anyone know why it causes an error with task scheduler but not when the batch file is manually executed?

python "C:\Users\XXX\untitled0.py" >log.txt 2>&1

CodePudding user response:

A scheduled task runs with a working folder of (usually) C:\Windwos\System32, which is write protected.

Give the full path: ... > "%userprofile%\xxx\log.txt 2>&1
or do a cd /d "%~dp0 before to switch to the folder where your script is located.

CodePudding user response:

0x1 run error is cause by privilege error. In your case, the issue is probabily the fact the Task Scheduler lacks the privilege to write the log.txt file infact, as you have described, running the script with no >log.txt 2>&1 has no issue.

Move the script to "C:\untitled0.py" and run it with the Task Scheduler from there

  • Related