file.close() does not really close under Windows?

Clovis Fabricio nosklo at gmail.com
Thu Dec 17 09:25:17 EST 2009


Hello Dani,

2009/12/17 Dani <daninanz at gmail.com>:
> Is it correct that low-level file handles are not being closed after
> doing
>  fd = open(filepath)
>  fd.close()
> If so, what is the rationale?

No, it is incorrect. I tested that exact snippet here and it correctly
closes the file. I can move the file around just after that.

There must be something wrong elsewhere on your code.

That said, you could use the "with" statement in python >2.5 to make it clearer:

with open(filepath) as fd:
   # ... do stuff with fd ...

The file will be closed at the end of the with block.

nosklo



More information about the Python-list mailing list