Page 1 of 1

Writing data to a text file

Posted: Sun Jul 23, 2017 3:27 pm
by nmstewart
Hello world!

I am new to coding and Rasperry Pi. I have been working on a Python script that records Temperature and Humidity from the SenseHat and the CPU temp at a specified interval.

I can get the data to display on the sensehat display as well as on the shell screen. I was looking for a simple way to take the data from the shell and put it into a text file.

All of the tutorials I have seen are either way over my head or did not work in Idle3.
Here is my code

Code: Select all

from sense_hat import SenseHat
import time
import datetime
from time import sleep
import os
sense = SenseHat()
sense.set_rotation(180)
sense.low_light = True
delay=0
while True:
    def CPU_temp():
        temp = os.popen('vcgencmd measure_temp').readline()
        return(temp.replace("temp=","").replace("'C\n",""))
    hour = time.localtime().tm_hour
    minute = time.localtime().tm_min
    sec = time.localtime().tm_sec
    temp = round(sense.get_temperature()*1.8+32)
    humidity = round(sense.get_humidity(),1)
    cputmp = round(float(CPU_temp()))
    
    sleep(delay)
    delay=44
    
    message = "%d:%d:%d Temp %dF, humidity %d%%rh, CPU %dC"%(hour,minute,sec,temp,humidity,cputmp)

    print ("%d:%d:%d, %dF, %d%% %dC"%(hour,minute,sec,temp,humidity,cputmp))
    
    sense.show_message(message,scroll_speed=(0.08),text_colour=[255,255,0],back_colour=[0,0,0])
 
    



sense.clear()


and my shell displays this:
11:9:16, 90F, 37% 54C
11:10:19, 90F, 36% 54C
11:11:23, 90F, 37% 54C
11:12:27, 90F, 37% 54C
11:13:31, 91F, 35% 54C
11:14:35, 91F, 36% 54C

Is there a simple way to capture the shell results to a text file.

Thanks
Neil

Re: Writing data to a text file

Posted: Sun Jul 23, 2017 7:56 pm
by DougieLawson
python /home/pi/yourpythonprogram.py > /tmp/filewithresults.txt

Re: Writing data to a text file

Posted: Sun Jul 23, 2017 8:32 pm
by gkaiseril
The posted code uses a "pipe" to redirect the output that normally would go the the standard output (console) to a file. The provided line of script has to be executed to perform this task. You can run this from the terminal or create a script file that could be called to run it and make a menu item or desktop icon.

Re: Writing data to a text file

Posted: Sun Jul 23, 2017 8:50 pm
by nmstewart
DougieLawson wrote:python /home/pi/yourpythonprogram.py > /tmp/filewithresults.txt
I was hoping to find a way to append to a file within my script. I would like to remove the loop and create a crontab entry,

Re: Writing data to a text file

Posted: Sun Jul 23, 2017 8:53 pm
by nmstewart
gkaiseril wrote:The posted code uses a "pipe" to redirect the output that normally would go the the standard output (console) to a file. The provided line of script has to be executed to perform this task. You can run this from the terminal or create a script file that could be called to run it and make a menu item or desktop icon.

Thank you for your clarification. Was my original post offensive in any way?

Re: Writing data to a text file

Posted: Sun Jul 23, 2017 11:49 pm
by gkaiseril
The ">>" is the pipe for apoend to a file or create file if the file does not exist. I have no matter what zoS one is workig with it is a good idea to learn the badic syntax of the vommand line.

Re: Writing data to a text file

Posted: Mon Jul 24, 2017 1:53 am
by nmstewart
gkaiseril wrote:The ">>" is the pipe for apoend to a file or create file if the file does not exist. I have no matter what zoS one is workig with it is a good idea to learn the badic syntax of the vommand line.
Thanks
The double pipe worked.The single just overwrote what existed.

:oops:

Re: Writing data to a text file

Posted: Mon Jul 24, 2017 7:01 am
by bensimmo
To learn how to do it in the program (and a bit of file I/O)
Have a look at
https://www.raspberrypi.org/learning/se ... worksheet/

Just in case you ever need it.

P.S. do you want help with the code you are using.
You have a function defined in a loop.
You are importing time and then importing part of it again later.
You are importing datetime but I don't think it is used
and the delay= just seems a little wrong