Score:1

My cron doesn't find a file

id flag

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)
vn flag
Does this answer your question? [Cannot create a crontab job for my scrapy program](https://askubuntu.com/questions/1288111/cannot-create-a-crontab-job-for-my-scrapy-program)
Score:3
vn flag

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.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.