I want to put the date and time into a file. Below is what I have tried.

import time
import datetime
>>> test_time = open('test.time', 'w')
>>> test_time.write('today is')
>>> test_time.close()
>>> test_time = open('test.time', 'a')
>>> today = datetime.date.today()
>>> print today
2008-03-09
>>> test_time.write(today)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: argument 1 must be string or read-only character buffer, not datetime.date
>>>

My question is :
In above case, how to put the value of datetime.date.today() into the file ?

Thank you for your help !!

If you're on Linux/UNIX, you can use a bash command to accomplish this, with:

#!/usr/bin/bash/
import os
os.system(datetime + ">file.txt")

write(s) expects a string, so use str(today) for s:

import datetime

test_time = open('test.time', 'w')
test_time.write('today is\n')
test_time.close()

test_time = open('test.time', 'a')
today = datetime.date.today()
print today, type(today)  # 2008-03-09 <type 'datetime.date'>

# convert <type 'datetime.date'> to a string first
# since write() expects a string
test_time.write(str(today))

test_time.close()

# test the file's contents
for line in file('test.time'):
    print line,

Thx Linux and Zzucker !!

It happened something strange and below is what I have tried. Hope you may tell me what I have done wrong.

I made a abc.py with code as :
#!/usr/bin/python

import datetime
test_time = open('test.time', 'w')
test_time.write('oh, today is\n')
test_time.close()

test_time = open('test.time', 'a')
timing = datetime.date.today()
#
print timing, type(timing) # 2008-03-09 <type 'datetime.date'>
# convert <type 'datetime.date'> to a string first
# since write() expects a string
test_time.write(str(timing))
test_time.close()

# test the file's contents
for line in file('test.time'):
print line,

At first, I run it in a folder called cx_Freeze-3.0.3. I dont know if it is the effect of this folder, here is the result aftering run.

~/cx_Freeze-3.0.3$ ./abc.py
Traceback (most recent call last):
File "./abc.py", line 9, in <module>
timing = datetime.date.today()
File "time.py", line 9, in <module>
today = datetime.date.today()
TypeError: attribute of type 'module' is not callable
Error in sys.excepthook:
Traceback (most recent call last):
File "/var/lib/python-support/python2.5/apport_python_hook.py", line 38, in apport_excepthook
from apport.fileutils import likely_packaged
File "/var/lib/python-support/python2.5/apport/__init__.py", line 1, in <module>
from apport.report import Report
File "/var/lib/python-support/python2.5/apport/report.py", line 14, in <module>
import subprocess, tempfile, os.path, urllib, re, pwd, grp, os, sys
File "/usr/lib/python2.5/urllib.py", line 28, in <module>
import time
File "time.py", line 9, in <module>
today = datetime.date.today()
TypeError: attribute of type 'module' is not callable

Original exception was:
Traceback (most recent call last):
File "./abc.py", line 9, in <module>
timing = datetime.date.today()
File "time.py", line 9, in <module>
today = datetime.date.today()
TypeError: attribute of type 'module' is not callable

Then I go to check its outcome, there is a file test.time but its content is only :today is. I have tried many times modifying the script such as deleting the first part or changing name to test.time1, however, the outcome is still the same : there is a file test.time but its content is only : today is

At last, I make a new folder called test_py_test and put the script abc.py there. After running, its outcome is what I expected. Also tried in the other folder, I am happy with all its result.

~/cx_Freeze-3.0.3/test_py_test$ ./abc.py
2008-03-10 <type 'datetime.date'>
oh, today is
2008-03-10

And I am happy as I find the content of test.time is :
oh, today is
2008-03-10

This article has been dead for over six months. Start a new discussion instead.