1

I have written a Python script that I would like to run by crontab! I have run the Python script individually and it works fine! The problem is that when I run it by Crontab, I receive an error indicating that one of the Python files that it called does not exist.

The error in /tmp/listener.log is as follows:

Traceback (most recent call last):
  File "/root/telegram/sendmsg.py", line 21, in <module> 
  file = open("List.csv") 
FileNotFoundError: [Errno 2] No such file or directory: 'List.csv'

Despite the fact that my file exists and it runs when I run my script without crontab!

I have my crontab set up like this:

30 14 * * * /usr/bin/python3 /root/tele/msg.py > /tmp/listener.log 2>&1

My Code:

import csv
from datetime import date
from timeIR import *
from persiantools.jdatetime import JalaliDate
from persiantools import characters, digits
import requests

def send_to_telegram(message):

apiToken = '****'
chatID = '***'
apiURL = f'https://api.telegram.org/bot{apiToken}/sendMessage'

try:
    response = requests.post(apiURL, json={'chat_id': chatID, 'text': message })
    print(response.text)

except Exception as e:
    print(e)

file = open("List.csv") data = csv.reader(file) data = csv.reader(file, delimiter=",")

whoisinshift = [] shiftname = [] phone = []

today=digits.fa_to_en(ShowDateDay())

for row in data: listdate = row[0] if listdate==today: mydate=digits.fa_to_en((ShowTodayFull())) whois=row[1] phonenumber=row[2] file.close()

mymsg=u"\U0001F4C5"+"\t"+mydate+"\n"+u"\uE13B"+"\t"+whois+"\n"+u"\U0001F4F1"+"\t"+phonenumber send_to_telegram(mymsg)

Artur Meinild
  • 26,018

1 Answers1

3

As the error message clearly states, the problem is that the script can't find the file List.csv (when run with Cron), because you haven't stated the full path.

Edit line 21 in your script to:

file = open("/root/telegram/List.csv")

Then your script will work, even with Cron.

Artur Meinild
  • 26,018