I have a python script that uses two functions but one of these does not work as desired.
main() calls the first function Define_and_Collect_1_WireData():
and this works as intended. However I am unable to pass the string variable data_stream_logfile
from function Define_and_Collect_1_WireData():
to function log_file()
.
I have not used return
as I am not certain that this keeps the variable local rather than global.
I have tried many combinations / variations available on this site: none that I could understand worked.
How can I do this?
#!/usr/bin/env python
import time
# Create a useful time stamp for logging to the log file
timestamp = time.strftime("%d/%m/%Y %H:%M:%S")
def Define_and_Collect_1_WireData():
tfile = open("/sys/bus/w1/devices/28-0000052c29e1/w1_slave")
text = tfile.read()
tfile.close()
temperature_data = text.split()[-1]
temperature = float(temperature_data[2:])
temperature1 = round(temperature / 1000,1)
#for the purposes of testing. Temperature1 will be pulled from the Dallas 1-wire bus
temperature1=13.1
# Create data_stream_logfile string
data_stream_logfile = str(temperature1) + "," + str(timestamp)
# Print output to a terminal
print "Raw temperature data as written to log file:"
print data_stream_logfile
print " "
def log_file():
# Open the text log datafile
datafile_temperature_log = open("log_cron_temperature_data.log", "a", 1)
datafile_temperature_log.write(data_stream_logfile + "\n")
datafile_temperature_log.close()
def main():
Define_and_Collect_1_WireData()
log_file()
main()