cannot write to file after close()

R

Rainer Hubovsky

Hello Python-Gurus,

==========================
f = open(LOGFILE,'w')
f.write(time + '\n')
f.close

command = 'ping -n 20' + target + '>>' + LOGFILE
system(command)
==========================

produces an error saying that a file cannot be accessed because it is used
by another process. I asume it is f which is used but don't understand why.

Any ideas?

Thanks a lot!

Cheers,
Rainer
 
R

Reinhold Birkenfeld

Rainer said:
Hello Python-Gurus,

==========================
f = open(LOGFILE,'w')
f.write(time + '\n')
f.close

command = 'ping -n 20' + target + '>>' + LOGFILE
system(command)
==========================

produces an error saying that a file cannot be accessed because it is used
by another process. I asume it is f which is used but don't understand why.

Any ideas?

Is the above exactly your code? If yes, it should be

f.close()

The parentheses are necessary to make the statement a function call.

Reinhold
 
R

Rainer Hubovsky

Thank you Reinhold, that was the solution. But just because I am curious:
what is this statement without the parentheses? After all it is a valid
statement...

Rainer
 
F

Fredrik Lundh

Rainer said:
Thank you Reinhold, that was the solution. But just because I am curious:
what is this statement without the parentheses? After all it is a valid
statement...

it's an expression that fetches the "close" method object, and throws
it away. to see what it evaluates to, try running the code from the
interactive prompt (or add a print statement):
<built-in method close of file object at 0x00836B20>

also see

http://docs.python.org/ref/exprstmts.html
http://docs.python.org/ref/attribute-references.html

</F>
 
R

Reinhold Birkenfeld

Rainer said:
Thank you Reinhold, that was the solution. But just because I am curious:
what is this statement without the parentheses? After all it is a valid
statement...

Rainer

In addition to what Fredrik said, this can be useful for shortcutting a name,
such as

c = f.close
c()

Reinhold
 

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

Forum statistics

Threads
474,264
Messages
2,571,323
Members
48,007
Latest member
Elvis60357

Latest Threads

Top