1

I have problems to close a file created in GDAL

I have the following code (it's a part of bigger piece of code):

driver = gdal.GetDriverByName("GTiff")
SAL01=driver. Create(Fnout1,afile.shape[1],afile.shape[0],1,tipoarr)
SAL01.SetProjection(proj)
SAL01.SetGeoTransform(trans)
SAL01.GetRasterBand(1).SetNoDataValue(10000)
band = SAL01.GetRasterBand(1)
band.WriteArray(afile)
band.FlushCache()
band.ComputeStatistics(False)
Fnout.close()

The error when process Fnout.close() is 'str' object has no attribute 'close'

In fact, Fnout is a string (used to generate the file name) and not a 'file' object.

Any simple way to close the file using the string as reference?

1
  • Please always include the version of whatever software you are using when asking a question.
    – user2856
    Commented Sep 29, 2023 at 1:36

1 Answer 1

4

For gdal >= 3.8:

Use Close not close (case sensitive). Or use a with statement as 3.8 Dataset objects support context managers.

SAL01.Close()

If using an older gdal, delete or set SAL01 to None to dereference it. The dataset will be closed and written.

SAL01 = None
# or
del SAL01
3
  • Thanks, but doesn't work. Appear the message 'Dataset' object has no attribute 'Close'
    – Eduardo
    Commented Sep 28, 2023 at 21:48
  • @Eduardo see edit
    – user2856
    Commented Sep 28, 2023 at 21:58
  • Thanks!!!. SAL01=None works!
    – Eduardo
    Commented Sep 29, 2023 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.