0

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

2
  • Show us the code fo td.registerDeviceEvent(myDeviceEvent). Commented Oct 17, 2013 at 7:18
  • What's td.registerDeviceEvent(myDeviceEvent) return value and type? Commented Oct 17, 2013 at 7:19

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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')))

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.