[newbie] file object not returned by function?

From: Eur van Andel (eur_at_fiwihex.nl)
Date: 10/31/03


Date: Fri, 31 Oct 2003 12:44:58 +0100

Hi

I tried this:

>from time import strftime, localtime
>
>
>def make_logfile():
> logfile = open(strftime("%d-%m-%Y_%H-%M.log", localtime()),'w')
> logfile.write('temperatures and pump duty cycles\n')
> logfile.write('from six fans and pump controller\n')
> return(logfile)
>
>
>logfile = make_logfile
>
>T1 = 20
>T2 = 30
>T3 = 40
>
>logfile.write("T1, T2, T3") # I know this does not work :-)

Traceback (most recent call last):
  File "file_try.py", line 19, in ?
    logfile.write("T1, T2, T3")
AttributeError: 'str' object has no attribute 'write'

???

This works:

>from time import strftime, localtime
>
>logfile = open(strftime("%d-%m-%Y_%H-%M.log", localtime()),'w')
>logfile.write('temperatures and pump duty cycles\n')
>logfile.write('from six fans and pump controller\n')
>
>T1 = 20
>T2 = 30
>T3 = 40
>
>logfile.write("T1, T2, T3")

Why can't a function return a file object?

--
Ir. E.E. van Andel, Fine Wire Heat Exchangers, Fiwihex B.V. www.fiwihex.com
Wierdensestraat 74, NL-7604 BK  Almelo, The Netherlands   eur@fiwihex.nl
phone +31-546-491106  fax +31-546-491107  mobile +31-653-286573