User avatar
nmstewart
Posts: 15
Joined: Sun Jul 23, 2017 2:36 pm
Location: River Vale NJ US

Writing data to a text file

Sun Jul 23, 2017 3:27 pm

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

User avatar
DougieLawson
Posts: 40554
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Writing data to a text file

Sun Jul 23, 2017 7:56 pm

python /home/pi/yourpythonprogram.py > /tmp/filewithresults.txt
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

gkaiseril
Posts: 679
Joined: Mon Aug 08, 2016 9:27 pm
Location: Chicago, IL

Re: Writing data to a text file

Sun Jul 23, 2017 8:32 pm

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.
f u cn rd ths, u cn gt a gd jb n cmptr prgrmmng.

User avatar
nmstewart
Posts: 15
Joined: Sun Jul 23, 2017 2:36 pm
Location: River Vale NJ US

Re: Writing data to a text file

Sun Jul 23, 2017 8:50 pm

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,

User avatar
nmstewart
Posts: 15
Joined: Sun Jul 23, 2017 2:36 pm
Location: River Vale NJ US

Re: Writing data to a text file

Sun Jul 23, 2017 8:53 pm

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?

gkaiseril
Posts: 679
Joined: Mon Aug 08, 2016 9:27 pm
Location: Chicago, IL

Re: Writing data to a text file

Sun Jul 23, 2017 11:49 pm

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.
f u cn rd ths, u cn gt a gd jb n cmptr prgrmmng.

User avatar
nmstewart
Posts: 15
Joined: Sun Jul 23, 2017 2:36 pm
Location: River Vale NJ US

Re: Writing data to a text file

Mon Jul 24, 2017 1:53 am

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:

User avatar
bensimmo
Posts: 5055
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Writing data to a text file

Mon Jul 24, 2017 7:01 am

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

Return to “Python”