简体   繁体   中英

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

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)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM