PermissionError: [WinError 32] with fits files

PermissionError: [WinError 32] with fits files

我正在尝试使用此代码和 shutil 程序包读取 FITS 文件并将其从一个文件夹移动到另一个文件夹 :

    MATISSE_DIR_N     = MATISSE_DIR+'N'  
    MATISSE_DIR_LM    = MATISSE_DIR+'LM'
    MATISSE_DIR_TRASH = MATISSE_DIR+'TRASH' 
    
        
    for filenames in glob.glob(MATISSE_DIR+'*.fits'):

        print(filenames)
        FOLDER_FLAG_LM    = False 
        FOLDER_FLAG_N     = False
        FOLDER_FLAG_TRASH = False
        
        if 'IR-N' in filenames:
            FOLDER_FLAG_N = True
        elif 'IR-LM' in filenames:
            FOLDER_FLAG_LM = True

        
        fichier = fits.open(filenames)    
        
        
        # VISIBILITY
        
#        Visibility_2_fichier = fichier["OI_VIS2"].data["VIS2DATA"]


        fichier.close()

        if np.logical_and(FOLDER_FLAG_TRASH==False,FOLDER_FLAG_N==True):
            shutil.move(filenames,MATISSE_DIR_N+'/')
        
        elif np.logical_and(FOLDER_FLAG_TRASH==False,FOLDER_FLAG_LM==True):
            shutil.move(filenames,MATISSE_DIR_LM+'/')
            
        elif FOLDER_FLAG_TRASH == True :
            shutil.move(filenames,MATISSE_DIR_TRASH+'/')

这非常有效,但是当我取消注释注释行时:

        Visibility_2_fichier = fichier["OI_VIS2"].data["VIS2DATA"]

这不再有效并出现以下错误:

runfile('C:/Users/jdrevon/Desktop/THESE/Modeling/DATA_SORTING/untitled0.py', wdir='C:/Users/jdrevon/Desktop/THESE/Modeling/DATA_SORTING')
C:/Users/jdrevon/Desktop/THESE/DATA/DATA_RSCL_test/NOMEANBCD\NAMEOFMYFILE
Traceback (most recent call last):

  File "C:\Users\jdrevon\anaconda3\lib\shutil.py", line 788, in move
    os.rename(src, real_dst)

PermissionError: [WinError 32] Le processus ne peut pas accéder au fichier car ce fichier est utilisé par un autre processus: 'C:/Users/jdrevon/Desktop/THESE/DATA/DATA_RSCL_test/NOMEANBCD\NAMEOFMYFILE' -> 'C:/Users/jdrevon/Desktop/THESE/DATA/DATA_RSCL_test/NOMEANBCD/LM/NAMEOFMYFILE'


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "C:\Users\jdrevon\Desktop\THESE\Modeling\DATA_SORTING\untitled0.py", line 76, in <module>
    V2_MATISSE,UV, UV_TP,TP_MATISSE,FLUX_MATISSE = OIFITS_READING(MATISSE_DIR)

  File "C:\Users\jdrevon\Desktop\THESE\Modeling\DATA_SORTING\untitled0.py", line 64, in OIFITS_READING
    shutil.move(filenames,MATISSE_DIR_LM+'/')

  File "C:\Users\jdrevon\anaconda3\lib\shutil.py", line 803, in move
    os.unlink(src)

PermissionError: [WinError 32] Le processus ne peut pas accéder au fichier car ce fichier est utilisé par un autre processus: 'C:/Users/jdrevon/Desktop/THESE/DATA/DATA_RSCL_test/NOMEANBCD\NAMEOFMYFILE'

我不明白为什么当您开始存储来自数据文件的数据时,fichier.close() 命令不再足以关闭文件。我已经尝试过 with 版本的代码来打开文件,但这并没有改变任何东西。

This warning in the documentation 应该回答您的问题(也许应该将警告移到别处,因为它不仅仅针对“大文件”):

When opening a file with memmap=True, because of how mmap works this means that when the HDU data is accessed (i.e., hdul[0].data) another handle to the FITS file is opened by mmap. This means that even after calling hdul.close() the mmap still holds an open handle to the data so that it can still be accessed by unwary programs that were built with the assumption that the .data attribute has all of the data in-memory.

In order to force the mmap to close, either wait for the containing HDUList object to go out of scope, or manually call del hdul[0].data. (This works so long as there are no other references held to the data array.)