Sunday, February 20, 2022

[SOLVED] Automatic script doesn't output data

Issue

I am just for fun collecting weather data with my Raspberry Pi. If I execute my python script in the console everything is working fine.But if I add the python-file to crontab to start it after rebooting, it isn't working. (crontab-entry: @reboot python3 /home/pi/Documents/PythonProgramme/WeatherData/weatherdata.py &)

#! /usr/bin/python3

from pyowm import OWM
import csv
import schedule
from datetime import datetime
import time

key = 'XXXXXX'


def weather_request(text):

    owm = OWM(key)
    mgr = owm.weather_manager()

    karlsruhe = mgr.weather_at_place('Karlsruhe, DE').weather
    hamburg = mgr.weather_at_place('Hamburg, DE').weather

    cities = (karlsruhe, hamburg)

    with open('weatherdata.csv', 'a') as file:
        writer = csv.writer(file)
        row = [datetime.now().strftime("%Y-%m-%d %H:%M:%S")]
        for city in cities:
            row.append(city.temperature('celsius')['temp'])
        row.append(round(row[1] - row[2], 2))
        row.append(text)
        writer.writerow(row)


schedule.every().day.at("08:00").do(weather_request, 'morgens')
schedule.every().day.at("13:00").do(weather_request, 'mittags')
schedule.every().day.at("18:00").do(weather_request, 'abends')


while 1:
    schedule.run_pending()
    time.sleep(1)

If I run ps -aef | grep python it is showing, that my script is running: pi 337 1 21 10:32 ? 00:00:10 python3 /home/pi/Documents/PythonProgramme/WeatherData/weatherdata.py

But I never get any data. What am I missing?

Thanks in advance!


Solution

where are you checking the output file? Have tried to open the file with full path?

with open('***<fullPath>***weatherdata.csv', 'a') as 


Answered By - GManika
Answer Checked By - Katrina (WPSolving Volunteer)