I'm trying to write this

print dt(), socket.gethostname(), " at ", getIP('eth0')
print "---------------------------------------------------------"
print "Temperature: ", CPU_temp,"F"
print "CPU Usage: ", CPU_Usage
print "Current RAM usage is: ", RAM_used, " Used. Of ", RAM_total, " RAM free is ", RAM_free
print "Current size of disk is: ", DISK_total, " Size of disk used ", DISK_used, " size of disk remaining ", DISK_free

using with open

with open("/home/pi/Desktop/stats.txt", "a") as log:
    data = write_stats()

but can only write it to a console window

closed as off-topic by Ingo, Milliways, Dmitry Grigoryev, Ghanima Jan 7 at 22:56

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be specific to the Raspberry Pi within the scope defined in the help center." – Ingo, Milliways, Dmitry Grigoryev, Ghanima
If this question can be reworded to fit the rules in the help center, please edit the question.


Use a function that accepts multiple parameters:

def log(*params):
    with open("/home/pi/Desktop/stats.txt", "a") as log:
        for param in params:
            log.write(str(param)+' ')


Resulting file:

a b c d 
e 1 f 
g 1.1

Simplest solution without rewriting your code is to direct stdout to a file with bash redirection >.

python3 mypythonprogramwithprint.py > /home/pi/desktop/stats.txt

If you want to write the file in python you'll need to change all of those print calls into log.write(...) calls.

  • how when I just tried it on the first one I get a function takes exactly 1 argument (4 given) ?? – bckelley Jan 7 at 0:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.