2

我在 Raspberry Pi 上有以下脚本来发送 SMS。如果我输入它就会运行:python潮汐_sms.py

问题是,我无法通过 Crontab (* * * * * /usr/bin/python /home/pi/python_files/tides_sms.py) 运行它。文件设置为:rwxr-xr-x

当我添加代码以写入文件时,该文件是通过 Crontab 创建的,但它不会发送 SMS。

任何帮助表示赞赏。


#!/usr/bin/python

from twilio.rest import TwilioRestClient

# Your Account Sid and Auth Token from twilio.com/user/account
account_sid = "**********************************"
auth_token = "********************************"

with open("tide_data.txt", "r") as file:
    tides_array = file.read().splitlines()

tides_array.reverse()

elements = tides_array[0].split(' | ')

string=''
for element in elements:
    string = '\n'.join([string, element])

client = TwilioRestClient(account_sid, auth_token)

message = client.sms.messages.create(body="Text from PI:\nTIDES" + string,
    to="+44??????????",
    from_="+44??????????")
4

1 回答 1

3

当脚本通过 cron 运行时,工作目录是/- 文件系统根目录。在脚本中使用绝对路径:

with open("/path/to/tide_data.txt", "r") as file:
于 2013-09-28T08:55:08.983 回答