Print output to file on python
Question: 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


Answer: 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)
    

