Linked Questions

Popular Questions

task scheduler and abspath

Asked by At

I've got a task to write a script to automate some routine operation - collect data from various sources, prepare a report based on gather data, and send it out. Nothing too sophisticated. My approach was to create a script, converts it to .exe, and schedule it to run daily. This script uses a few files to store data from previous day and is accessed during report preparation.

Here is a snippet that I sue to determine the filepath(s):

self._p_day_queues = os.path.abspath(os.getcwd() + f_path + " -- "+ dt.now().strftime(self._date_format) + ".csv")

When I converted it to .exe and ran it with double click - no problems, runs as expected. When it comes to scheduling it raises an error:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Windows\\' + my_file(s)_name
[13848] Failed to execute script ...

Apparently, if this file is executed by the task scheduler, the above code snippet treats scheduler's directory as its pwd and that messes up the abspath for files.

With that assumption, I fixed the issue using from pathlib import Path like this: Path.home() / Path("my_path_to_file"), but now its bond to one specific directory which is C:\\Users\\user_name.

How to fix it so .exe would become more flexible and will not dependent on one specific location?

Related Questions