Search code examples
pythonscheduled-taskswindows-task-scheduler

Why does Task Manager not run some lines of code in script?


I have a Python script that performs some geodatabase management (reconcile/post versions, compress, etc). I have the following line of code in my script:

createLog = open(str(datetime.date.today()) + ".txt", "w")

along each step of the script I add to the text file with the following statements:

createLog.write("Database connections blocked.\n")

When I run the script in my IDE (PyCharm) I get the desired result: A text file with each step written to the .txt file. When I run it in Task Scheduler no .txt file is created and therefore no log. Everything else runs as far as I can tell. I'm able to track edits made to the data.

I have experienced things like this before with task scheduler but have never been able to resolve the problem in the past.

Any ideas?


Solution

  • I think this is a working directory problem. Python's open function opens a file in the current working directory, NOT in the same folder as the script. This is a common misconception! (Which confused me for ages when learning Python...)

    So what is a working directory? Well to quote my good friend Wikipedia:

    In computing, the working directory of a process is a directory of a hierarchical file system, if any,[1] dynamically associated with each process. When the process refers to a file using a simple file name or relative path (as opposed to a file designated by a full path from a root directory), the reference is interpreted relative to the current working directory of the process. So for example a process with working directory /rabbit-shoes that asks to create the file foo.txt will end up creating the file /rabbit-shoes/foo.txt.

    (Source: https://en.wikipedia.org/wiki/Working_directory)

    So how is this working directory selected?

    Well it is selected by the parent process of that processes! When you run a program from a shell like bash, the shell (the parent process) helpfully sets the working directory of the program you are running (the child process) to the directory you are currently in. (That is, the directory you cd'd to.)

    Since your IDE is smart and helpful, it is starting your Python script process and setting the working directory to the same place the script itself is located. The task scheduler is less helpful... I have absolutely no idea what it is setting the working directory to. However if you search your system, I am sure you will find the log file lying about somewhere!