Why doesn't my Python script save the file when I use Task Scheduler?

1

1

I have a Python script that uses csv.writer to create a csv file. This works fine when I run it from the command line, but no file is created when I run the script using Windows Task Scheduler. The only thing I can think of is that I'm not running my computer as admin (because it's a work computer), but why would it let me do this on command line but not in Scheduler? The rest of the script runs with Scheduler, just not the csv file saving part.

Abbie

Posted 2015-04-17T14:58:41.470

Reputation: 295

Try to change the task's user under which it runs. Also, you haven't mentioned or tagged what Windows / OS you're using. – JasonXA – 2015-04-17T15:01:01.120

I can't change the user because I am on a work computer. I'm using Windows 7. – Abbie – 2015-04-17T15:52:39.997

@Abbie let me rewrite your script to powershell or WSH) – STTR – 2015-04-17T17:41:00.423

No Abbie, when setting up the task, in the properties there's a user under which it can run, try to change that. It's under the General tab as posted below in the screenshot. If you run as a limited user, then the write operations might fail just because there are no relevant privileges for the operation. Try to run it under your user and make sure the csv file is written to a location where you're allowed to write. – JasonXA – 2015-04-18T10:57:15.113

I was already running it under my user profile. What I don't understand is that there aren't privilege problems writing the csv file when I run this script myself from the command line but, when I use Task Scheduler, it doesn't write the csv file. – Abbie – 2015-04-20T08:10:30.263

What is the command you are using in python to write the .csv file? – law10 – 2015-11-24T21:07:44.813

@law10 I'm opening the file using report = open(filename, 'w') and closing it at the end using report.close() – Abbie – 2015-11-25T08:50:21.230

Answers

3

I used this to solve a similar problem. Under your batch action I set it up as the following and it worked fine for me.

  • Program/script: path to python, e.g., C:\Python27\ArcGIS10.2\python.exe
  • Add Arguments (optional): script name, e.g., scrape.py
  • Start in (optional): path to script, e.g., C:\Users\handsome\Documents\PYTHON\Code

Dave

Posted 2015-04-17T14:58:41.470

Reputation: 31

-2

Program/script: cmd arg: "/c C:\Python27\python.exe C:\Script\script.py"

enter image description here

STTR

Posted 2015-04-17T14:58:41.470

Reputation: 6 180

2You got the downvotes because you didn't put the least bit of effort into your answer. All you did was drop some cryptic command without explaining what it is or what to do with it. It took me more time to understand your answer than it did to understand the poster's question, and the only reason I spent that time was because it came up in the Low Quality Posts review queue. I edited your answer to make it more clear. All I did was add a screenshot that took me less than 30 seconds to make. – Wes Sayeed – 2015-04-17T22:26:52.230

Thanks for the suggestion (and the added screenshots). I tried this way but, when I pressed Run, the command box opened for half a second then closed and it didn't look like anything happened. – Abbie – 2015-04-20T08:06:05.440