I'm a py newbie and was wondering if there was a simpler way to concatenate time to a string in a write function? here is my code running windows xp with activepy 2.6:
from time import clock
filename = "c:\Python\\test.txt"
try:
tm = clock()
print "filename: " + filename
fsock = open(filename, "a")
try:
fsock.write(tm + 'test success\n ')
finally:
fsock.close()
except IOError:
print "file not found"
print file(filename).read()
C:\Python>python test.py
filename: c:\Python\test.txt
Traceback (most recent call last):
File "test.py", line 8, in <module>
fsock.write(tm + 'test success\n ')
TypeError: unsupported operand type(s) for +: 'float' and 'str'
C:\Python>
time.clock
returns a machine-readable representation of the duration the system is running.
To get a human-readable representation (a string) of the current wall time, use time.strftime
:
>>> import time
>>> tm = time.strftime('%a, %d %b %Y %H:%M:%S %Z(%z)')
>>> tm
'Mon, 08 Aug 2011 20:14:59 CEST(+0200)'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With