我有一个.py脚本,当我在控制台中运行它时可以工作,但是当我将它安排为自动任务时似乎失败了。该脚本使用urllib.request(我确保将Hashbang设置为运行支持该软件包的Python 3.6)来命中并从特定URL下载文件。再次手动运行时没有问题,但是当它运行时计划任务我收到此错误:
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.
它指向的代码片段如下(出于安全目的隐藏完整的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)
最后一行是回溯发生的地方。请帮助我理解为什么在我在控制台中运行时运行,但在我将其作为任务安排时失败。
答案 0 :(得分:1)
非常愚蠢,但我只是想通了。当计划任务运行时,它会从导致问题的其他目录运行。
感谢pythonany上的giles让我失望:https://www.pythonanywhere.com/forums/topic/12790/#id_post_49647
需要cd到任务计划程序中的正确目录:
cd /home/laiAdmin/something/something; python3.6 /home/laiAdmin/myscript.py