Posts: 10
Joined: Fri Jun 19, 2020 12:03 pm

Help with Code

Sun Feb 14, 2021 1:44 am


I need help with something. My python code's directory is /home/pi/ I have set it up using rc.local to run on boot. Now, when I run this program myself from within the programs directory it saves TotalHours.txt to the current directory. When I reboot the raspberry pi and it starts running the program in the background it saves TotalHours.txt to the parent directory so it is just /TotalHours.txt. What I would like to do is to have it ALWAYS save TotalHours.txt to the /home/pi/TotalHours.txt. Additionally is there a way to make this program run constantly in the background so that when I open up the raspberry command line and do other things it does not interrupt this program and if it does it automatically starts back? The purpose of the program is to count and add up the number of hours in the year that the temperature is between 32 and 45 degrees so it is setting on my covered porch but Id like to be able to ssh in and do other things with it from time to time like say Check the TotalHours.txt file without interuppting. Anyway thanks

Code: Select all

import adafruit_dht
from board import pin
from time import sleep

# Initialize the total hours

while True:
        # Open File for read only
        # Read File for initial total hours and convert string to integer
        total_hours= int(
        # Close File
        # Check Temperature
        # Convert temperature to fahrenheit
        #Increment Counter for chill hours
        if temperature_f>32 and temperature_f<45:
        # Open file for re-write
        file1= open("TotalHours.txt","w")
        # Re-write file and convert integer to string
        # Close file
        #Sleep for 1 hour

Posts: 7853
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Help with Code

Sun Feb 14, 2021 2:50 am

Use the full path in all of your open file statements.

Code: Select all

Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

Posts: 2001
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Help with Code

Sun Feb 14, 2021 6:10 am

For starting the program in the background, see ...
When running programs in background, consider to implement signal handler to gracefully terminate the program on shutdown:

User avatar
Posts: 2615
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Help with Code

Sun Feb 14, 2021 8:16 am

systemd is definitely the way to go. But IMO extremely hard to debug until you discover journalctl

Posts: 40
Joined: Tue Nov 24, 2020 4:44 pm

Re: Help with Code

Sun Feb 14, 2021 8:28 am

You could try this, it uses the scripts directory so would be more portable:

Code: Select all

import os

# Use paths relative to the script.
dirname = os.path.dirname(__file__)

f = open(dirname + '/file.txt', 'r')

# Do stuff with file contents.
As others have said use systemd for starting at boot.

Posts: 40
Joined: Tue Nov 24, 2020 4:44 pm

Re: Help with Code

Sun Feb 14, 2021 8:46 am

Also if this only runs every hour it strikes me that using cron to schedule it might be a better way to go: ... ge/

This way it could start and run every hour when the RPi is on. You would need to rewrite the script without the loop so it does everything it needs to for a single execution.

User avatar
Posts: 7193
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Help with Code

Sun Feb 14, 2021 10:31 am

There are many solutions :)
You could simply add your program to /etc/rc.local with a trailing & to run it in the background.
For example, my clock is started like this:

/home/pi/ &

Return to “Python”