Forums

URLRetrieve Issues when I schedule Task

I have a .py script that works when I run it in the console but seems to fail when I schedule it as an automatic task. The script uses the urllib.request (I made sure to set the Hashbang to run Python 3.6 which supports that package #!/usr/bin/python3.6) to hit and download a file from a specific URL.. Again no problems when I run it manually, but when its run as a scheduled task I get this error:

Traceback (most recent call last):
  File "/home/laiAdmin/Scripts/FullAutoSubStats.py", line 32, in <module>
    urllib.request.urlretrieve(URL, dlFileName)
  File "/usr/lib/python3.6/urllib/request.py", line 258, in urlretrieve
    tfp = open(filename, 'wb')
FileNotFoundError: [Errno 2] No such file or directory: '/home/laiAdmin/Data_Files/Downloads/FWdirect-Daily-Stats-Parser-autoDl-2018-05-08.json'

2018-05-08 16:48:11 -- Completed task, took 6.00 seconds, return code was 1.

The code snippet it is pointing to is the following (hiding the URL):

#Get timestamp and PWD
cwd = os.getcwd()
now = DT.datetime.utcnow()-DT.timedelta(hours=6)
todayStr = DT.datetime.strftime(now,"%Y-%m-%d")
nowStr = DT.datetime.strftime(now,"%m/%d/%Y %H:%M:%S")

#Request to download a new json file:
rootdir = cwd+'/Data_Files/Downloads/'
URL = "https://files.mailparser.io/d/*****" 
#(hiding the full URL here for security purposes)
fn = "FWdirect-Daily-Stats-Parser-autoDl-"+todayStr+".json"
dlFileName = rootdir+fn
urllib.request.urlretrieve(URL, dlFileName)

The last line is where the traceback takes place. Please help me to understand why this runs when I hit run in the console, but fails when I schedule it as a task.

Perhaps it's the working directory? Scheduled tasks will run in your home directory, /home/laiAdmin. When you run it in a console, are you cding to a different directory first? If so, you can do that in a schedule task, just schedule something like

cd /home/laiAdmin/something/something; python3.6 /home/laiAdmin/myscript.py

Note the ";" separating the cd and the python commands.

THat worked! thanks so much

Excellent! Thanks for confirming :-)