user2885442
user2885442

Reputation: 3

Print output to file on python

I have two python files. My test.py import td.py file witch i found internet. Td.py file looking signals from TelldusCenter program.

Now if i run test.py file it shows me signals what i get from TelldusCenter app and output is something like: "Door - ON" Now i like to print that "Door - ON" text to file but i dont know how.

Here is my test.py file

#!/usr/bin/env python

import td
import time


def myDeviceEvent(deviceId, method, data, callbackId):
    print '%s' %( td.getName(deviceId) )+' - %s' %(td.methodsReadable.get(method, 'Unknown' ))


td.registerDeviceEvent(myDeviceEvent)



try:
    while(1):
        time.sleep(1)
except KeyboardInterrupt:
            print 'KeyboardInterrupt received, exiting'

"td.registerDeviceEvent(myDeviceEvent)" print output to terminal now. I try to print that to file but it just give me error.

 a = open("output.txt", "w") 
 a.write(td.registerDeviceEvent(myDeviceEvent)) 

Traceback (most recent call last): File "testi.py", line 11, in a.write(td.registerDeviceEvent(myDeviceEvent)) TypeError: expected a character buffer object

Upvotes: 0

Views: 505

Answers (3)

thefourtheye
thefourtheye

Reputation: 239653

Change

def myDeviceEvent(deviceId, method, data, callbackId):
    print '%s' %( td.getName(deviceId) )+' - %s' %(td.methodsReadable.get(method, 'Unknown' ))

to

def myDeviceEvent(deviceId, method, data, callbackId):
    with open("Output.txt", "w") as outputFile:
        outputFile.write('%s' %( td.getName(deviceId) )+' - %s' %(td.methodsReadable.get(method, 'Unknown' )))

You can use with statement to handle files and its scope. You dont have to worry about the closing the file properly, when you use with. That takes care of it.

Edit: You can use modern string formatting like this. Read more about it here http://docs.python.org/2/library/string.html#string-formatting

def myDeviceEvent(deviceId, method, data, callbackId):
    with open("Output.txt", "w") as outputFile:
        outputFile.write('{} - {}'.format(td.getName(deviceId), td.methodsReadable.get(method, 'Unknown')))

Upvotes: 0

Vincent
Vincent

Reputation: 13425

You should consider the logging module with a basic configuration :

import logging
FORMAT = '%(asctime)s - %(message)s'
logging.basicConfig(format=FORMAT, filename='Output.txt', level=logging.INFO)

logging.info('My message')

File Output.txt :

2013-10-17 09:26:08,496 - My message

Upvotes: 0

Stefan Winkler
Stefan Winkler

Reputation: 3966

From my interpretation of the code, td.registerDeviceEvent(myDeviceEvent) registers a callback. It does not produce a string itself. This is why you cannot output the 'result' of the registration.

Instead try this:

#!/usr/bin/env python

import td
import time

a = open("output.txt", "w") 

def myDeviceEvent(deviceId, method, data, callbackId):
    a.write('%s' %( td.getName(deviceId) ) + ' - %s' %(td.methodsReadable.get(method, 'Unknown')

td.registerDeviceEvent(myDeviceEvent)

Upvotes: 1

Related Questions