Your original code:
As to the problem you face - when you use
def write_to_log(s): # open the file in append mode with open('/home/bs/NAS_2/projects/Raspberry_pi/Temperature/log.txt', 'a') as f: # write the line to the text file f.write('\n'.join(line));Note that your function expects parameter s, but it is never used. You actually use
line
and your code sort of works, only because there is line
variable in global scope.As to the problem you face - when you use
'\n'.join(some_string)
new-line is inserted between each char of the string>>> foo = 'spam' >>> eggs = '\n'.join(foo) >>> eggs 's\np\na\nm' >>> print(eggs) # same would be if you write to file s p a m >>>
def write_to_log(log_msg): # open the file in append mode with open('/home/bs/NAS_2/projects/Raspberry_pi/Temperature/log.txt', 'a') as f: # write the line to the text file f.write(f'{log_msg}\n')Right now the log file is hardcoded in the function, but you can pass it as argument.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs