Note that there are some explanatory texts on larger screens.

plurals
  1. POIs Python retaining a reference to a file opened in a list?
    text
    copied!<p>I have a program where I need to keep some objects that open files on disk list and delete those files after the program is done. However Python seems to be keeping the file open even though there are no more references to an object that should have it open. I've been able to recreate the problem with pure file objects below:</p> <pre><code>import os filenames = ['a.txt', 'b.txt'] files = [open(f,'w') for f in filenames] for f_object in files: f_object.write("test") del files[:] for name in filenames: os.remove(name) </code></pre> <p>When I run this on Windows I get the error </p> <pre><code>Traceback (most recent call last): File ".\file_del.py", line 11, in &lt;module&gt; os.remove(name) WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'b.txt' </code></pre> <p>Interesting that it is able to delete <code>a.txt</code> without a problem. What is causing <code>b.txt</code> file to be open even though the references to it are gone?</p> <p><strong>Update</strong></p> <p>In the original problem, I don't have access to the files to close them. Trust me, I would love to close those files. See the following:</p> <pre><code>base_uri = 'dem' out_uri = 'foo.tif' new_raster_from_base_uri(base_uri, out_uri, 'GTiff', -1, gdal.GDT_Float32) ds = [] for filename in [out_uri]: ds.append(gdal.Open(filename, gdal.GA_Update)) band_list = [dataset.GetRasterBand(1) for dataset in ds] for band in band_list: for row_index in xrange(band.YSize): a = numpy.zeros((1, band.XSize)) band.WriteArray(a, 0, row_index) for index in range(len(ds)): band_list[index] = None ds[index] = None del ds[:] os.remove(out_uri) </code></pre> <p><strong>Update 2</strong></p> <p>I've marked millimoose's answer as the correct one below since it fixes the issue with the abstracted problem of files that I presented here. Unfortuantely it didn't work with the GDAL objects I was using. For future reference, I dug deep and found the undocumented <code>gdal.Dataset.__destroy_swig__(ds)</code> function which seems to at least close the file that the dataset is associated with. I call that first before deleting the file on disk associated with the datasets and that seems to work.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload