Reputation: 11
I have a simple script that finds an excel file in a subfolder, opens it and converts it to a csv. I need to automate this to run every day at a certain time. I hoped to use Task Scheduler to do this.
I found this solution here: How to do a recursive sub-folder search and return files in a list?
list_of_files = glob.glob('sheets/**/*.xlsx', recursive=True)
latest_file = max(list_of_files, key=os.path.getctime)
filelocation = pathlib.Path().resolve()
When I compile and run my code as a user, it works fine. When I run it through task scheduler, it responds with the following error:
Traceback (most recent call last):
File "\\192.168.1.119\Work\exporttoact\ActImport.py", line 26, in <module>
latest_file = max(list_of_files, key=os.path.getctime)
ValueError: max() arg is an empty sequence
[16540] Failed to execute 'ActImport' due to unhandled exception!
I ~think~ this means, because I am running through task scheduler, that it does not have access to or cannot see that file share and is returning list_of_files as empty. Apparently, Task Scheduler opens each running script in a tmp folder in a protected space without the user environment.
How do I add that folder to my path so I am able to access it no matter where my code runs? I have tried to add via NET USE, but if the user already has the folder mapped (they do) it will not allow a second map.
Thanks!
Upvotes: 0
Views: 62
Reputation: 11
The answer was actually pretty simple.
In the program I am running, under Task Scheduler->Task->Actions I added the "Start In(Optional)" directory. It has to be where the .exe is located.
Upvotes: 1