2

Python novice here.

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?

Brad Jones
  • 33
  • 4

1 Answers1

1

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!

Others
  • 2,876
  • 2
  • 30
  • 52
  • That did it! Now I know and you saved me lots of future headache. – Brad Jones Dec 30 '15 at 16:18
  • Not just Python. I can't think of any common programming languages that would default to using relative paths from the binary instead of relative paths from the working directory. – ShadowRanger Dec 30 '15 at 16:19
  • @ShadowRanger Yep, I know that. I think it's pretty clear in the answer that this isn't just a Python thing. But if you think it is unclear then please edit away! – Others Dec 30 '15 at 16:32
  • The working directory for the task was C:\Windows\SysWOW64 – Brad Jones Dec 30 '15 at 16:32
  • @BradJones: That's what you'd expect running 32 bit Python on x86-64 Windows (assuming Windows installed to `C:`). If the bit-edness of Python matches the OS, it would be `C:\Windows\System32` instead. You can always check `os.getcwd()` to figure out the working directory in the general case. – ShadowRanger Dec 30 '15 at 16:36