2

I created a function where I access to a Raster folder and get all files CRS

            for Rsubdir, Rdirs, Rfiles in os.walk(Rrootdir):  
                for Rfile in Rfiles:
                    Rfile, Rextension = os.path.splitext(os.path.join(Rsubdir, Rfile).replace("\\", "/"))
                    if Rextension == '.tif':  
                        RfileName = os.path.join(Rsubdir, Rfile).replace("\\", "/") + ".tif"
                        if os.path.isfile(RfileName):
                            RfileInfo = QFileInfo(RfileName)
                            RbaseName = RfileInfo.baseName()
                            Rlyr = QgsRasterLayer(RfileName,RbaseName) 
                            coordR=Rlyr.crs().authid()
                            RasterCRS.append(coordR)

the problem is when I try to access the same folder files after this function call, I get a PermissionError

PermissionError: [WinError 32] Le processus ne peut pas accéder au fichier car ce fichier est utilisé par un autre processus: 'chemin_fichier.tif'

which means that the files are always opened. is there any way to close them in the end of the function?

1

I tried the del method and it's working

                for Rfile in Rfiles:
                    Rfile, Rextension = os.path.splitext(os.path.join(Rsubdir, Rfile).replace("\\", "/"))
                    if Rextension == '.tif':  # selectionner les ficheir tif dans le dossier 2018
                        RfileName = os.path.join(Rsubdir, Rfile).replace("\\", "/") + ".tif"
                        if os.path.isfile(RfileName):
                            RfileInfo = QFileInfo(RfileName)
                            RbaseName = RfileInfo.baseName()
                            Rlyr = QgsRasterLayer(RfileName,RbaseName)  # charger le fichier raster avec le chemin d acces obtenu
                            coordR=Rlyr.crs().authid()
                            RasterCRS.append(coordR)

                            del Rlyr #close file handler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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