[newbie] file object not returned by function?

Eur van Andel eur at fiwihex.nl
Mon Nov 3 04:26:04 EST 2003


On Fri, 31 Oct 2003 11:48:37 GMT, Alex Martelli <aleax at aleax.it> wrote:

>>>logfile = make_logfile
>Note that you're NOT calling the function, just assigning it to
>the name logfile.  Put parentheses after the functionname to call it.

On Sun, 02 Nov 2003 20:29:13 GMT, Dennis Lee Bieber <wlfraed at ix.netcom.com>
wrote:
>        logfile.write( "%s, %s, %s" % (T1, T2, T3) )

Thanks a lot. It works very well now.

>from time import strftime, localtime, sleep
>import random, fpformat
>
>def make_logfile():
>   #logfile = open(strftime("%d-%m-%Y_%H-%M.log", localtime()),'w')
>   logfile = open(strftime("%d_%H%M.log", localtime()),'w')
>   logfile.write('temperatures and pump duty cycles\n')
>   logfile.write('from six fans and pump controller\n')
>   return(logfile)
>
>date = localtime()[2]
>
>logfile = make_logfile()
>
>while True:
>   minutes, seconds =  localtime()[4], localtime()[5]
>
>   if date != localtime()[2]:   # a new day has dawned, open a new logfile
>      logfile = make_logfile()
>      date = localtime()[2]
>
>   T1 = 20 + random.uniform(0,5) - 2.5
>   T2 = 20 + random.uniform(0,5) - 2.5
>   T3 = 20 + random.uniform(0,5) - 2.5
>   T4 = 20 + random.uniform(0,5) - 2.5
>
>   if seconds % 5 == 0:
>      print strftime('"%d-%m-%Y %H:%M:%S" ', localtime()),
>      print '%2.2f %2.2f %2.2f %2.2f' % (T1, T2, T3, T4)
>      logfile.write(strftime('"%d-%m-%Y %H:%M:%S" ', localtime()))
>      logfile.write('%2.2f %2.2f %2.2f %2.2f' % (T1, T2, T3, T4)+'\n')
>      logfile.flush()
>      sleep(2)

The sleep(2) is for executing once during seconds % 5 == 0. The " are for
importing the date & time in Excel. 

Being a newbie, I have trouble finding examples of easy stuff. The library
reference is good, but lacks examples. The tutorial has some but I would like
to see more. 
--
Ir. E.E. van Andel, Fine Wire Heat Exchangers, Fiwihex B.V. www.fiwihex.com
Wierdensestraat 74, NL-7604 BK  Almelo, The Netherlands   eur at fiwihex.nl
phone +31-546-491106  fax +31-546-491107  mobile +31-653-286573  




More information about the Python-list mailing list