[Python-ideas] file(fn).read()

Josiah Carlson josiah.carlson at gmail.com
Sun Jun 14 20:26:16 CEST 2009


On Sat, Jun 13, 2009 at 8:05 AM, spir<denis.spir at free.fr> wrote:
> Le Sat, 13 Jun 2009 10:25:19 +0200,
> spir <denis.spir at free.fr> s'exprima ainsi:
>
>> Hello,
>>
>> this post is half a question, half a proposal.
>>
>> Q: what happens to the filesystem file, and to the python file object below?
>>
>>     text = file(fn).read()
>>
>> (used 'file' instead of 'open' in purpose)
>> Note that there seemingly is no way to close or del the file object, nor
>> even to check its 'closed' flag. I would expect that, in the case a file
>> remains unnnamed, it is automatically closed and del-ed. If not, do you
>> think it is sensible to ensure that?
>
> Actually, I could have answered my own question, at least for the current CPython implementation:
>
>>>> file("test.txt").read()
> 'foo\nbar\n'
>>>> file("test.txt",'r')
> <open file 'test.txt', mode 'r' at 0xb7dbbbf0>
>
> Seems to shows the file has been closed.

How so?  Reading a file isn't exclusive on any major Python platform.
You can get file locking on various platforms by using
platform-specific file locking calls, but that's not the default for
Python.

 - Josiah



More information about the Python-ideas mailing list