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.
3 replies
Yeah, `__file__` is the boring reliable fix here. Build both input and output paths from the script location so the task can start anywhere...
Yeah, `__file__` is the boring reliable fix here. Build both input and output paths from the script location so the task can start anywhere and still find the same files.
I’d still keep the scheduler “Start in” set correctly too, even if the script doesn’t rely on it. That way logs and any accidental relative paths are less likely to drift.
This. I have one backup script that worked for months until Task Scheduler decided it lived in `C:\Windows\System32` for reasons of spite. H...
You can also fail early if the file is missing, which sounds like what you want anyway. I usually make a tiny `base_dir = Path(__file__).resolve().parent` and then join everything off that.