Python script works manually but fails from Task Scheduler

I have a small Python script that reads a CSV, calls an API, then writes a result file. If I open a terminal and run it myself it works every time.

When Windows Task Scheduler runs the same script, it fails when it tries to open the CSV. The exception is basically “file not found.” The CSV is definitely there.

The file path in the script is currently relative, like `data/input.csv`. I checked the task history and noticed the scheduled task starts in a different working directory than the folder where the script lives. If I temporarily replace the path with the full absolute path, the task works.

So I think I understand the immediate cause. What I am less sure about is the clean way to structure this. I do not want to hard-code `C:\...` paths everywhere because I sometimes run the script on another machine.

Would you normally build paths from `__file__` / the script directory, put the data location in a config file, or set the scheduler's “Start in” directory and keep relative paths? The script also writes a log file, so I probably need to make the output location explicit too.

This is a small personal automation, not a deployed application. I am mainly trying to learn the boring reliable pattern so scheduled jobs do not depend on whichever folder happened to be current when they started.

One extra detail: the task runs under my normal user account, so I do not think permissions are the main issue. I also want the script to fail loudly if the input file is missing instead of silently creating files in some unexpected working directory. That is another reason I am leaning toward explicit paths.

  • python
  • debugging
  • automation
  • file-paths
21

4 replies · Sort: Best

Best

No conversations include a reply from the original poster.