[Tutor] a quick Q: how to use for loop to read a series of files with .doc end

Alan Gauld alan.gauld at btinternet.com
Fri Oct 7 21:00:02 CEST 2011

On 07/10/11 18:56, lina wrote:
> On Oct 8, 2011, at 0:39, "Prasad, Ramit"<ramit.prasad at jpmorgan.com>  wrote:
>  I would really change this to explicitly close the file.
>> with open(base+OUTFILEEXT,"w") as f:
>>     f.write(str(summary))
> Btw, I do notice lots of suggestions of closing file.
> Does your above sentence close the file here? Implicitly ?
> Sorry, I don't see "close"

The 'with' syntax above implicitly closes the file for you
at the end of the block. If you use the

f = open(...)

style you are expected to explicitly close the file when you are 
finished with it. This is especially important when writing to a file 
because that will guarantee that the OS writes the data to the file on 
disk. Without a close its possible for your data to  get held in a 
buffer that doesn't get written to disk. Rare, but possible.

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list