Forums

Can't get csv data to write on a scheduled task or always on task.

I have been trying over and over to get csv data to write on a task. I have tried python3, python3.8 and no luck either way. If I click run manually the csv will write but if it's on a scheduled task it won't. How can I fix this? Any help would be greatly appreciated! Thank you

def witness_query(statement, args = None): cursor = zohoDB.cursor() cursor.execute(statement, args)

if "select" == statement.split()[0]:
    result = cursor.fetchall()
    # print(result)
    c = csv.writer(open("witness_analytics.csv","w"))
    c.writerow(['record_id', 'address', 'witness_list', 'witness_count', 'lat', 'lng', 'accessed'])
    for row in result:
        # c.writerow(row)
        # print(row)
            c.writerow(row)
    cursor.close()
    return result


else:
    zohoDB.commit()
    cursor.close()

See https://help.pythonanywhere.com/pages/NoSuchFileOrDirectory/

It's the right folder, it writes when I run it cause I can find it in my folder on PA. It just doesn't work then the scheduled task runs

It works differently in the scheduled task because you are using a relative file path without paying attention to the working directory. That is what that help page describes and the fix that it provides.

this is what I have for my scheduling. I don't get what I'm doing wrong..

python3 /home/samwins/zoho/5g_statuses/radio_data/5g_radio_automation.py

If the CSV file is in the same directory as the script, then in a Bash console you would do this:

cd /home/samwins/zoho/5g_statuses/radio_data/
python3 5g_radio_automation.py

So in a scheduled task you should do the same thing, but separate the commands with a semicolon rather than a newline:

 cd /home/samwins/zoho/5g_statuses/radio_data/; python3 5g_radio_automation.py