[newbie] file object not returned by function?

E

Eur van Andel

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?
 
A

Alex Martelli

Eur said:
Hi

I tried this:

Note that you're NOT calling the function, just assigning it to
the name logfile. Put parentheses after the functionname to call it.
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 is absolutely unexplicable: logfile cannot be a str, it's
a function-object. Have you copy-and-pasted everything?!

Why can't a function return a file object?

Sure it can! Only if you call it, though -- if you don't call
the function it can't return anything.


Alex
 
D

Dennis Lee Bieber

Eur van Andel fed this fish to the penguins on Friday 31 October 2003
03:44 am:
Hi

I tried this:

logfile = make_logfile() #call it, not refer to it It wouldn't work any way, assuming you expect the output to
contain "20 30 40"; you are writing a literal string of "T1, T2, T3".
Try

logfile.write( "%s, %s, %s" % (T1, T2, T3) )

--
 
E

Eur van Andel

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

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.
 
R

Richard

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]


Just an idea here, but you call "localtime" four times here when you
only need to call it once. Might something like this work?

<code>
while True:
day, hours, minutes, second = localtime()[2:6]

if date != day:
logfile = make_logfile()
date = day
</code>

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top