import modin.pandas and ray() don't close file

163 Views Asked by At

I'm trying to use modin and ray() but I can't move file after read it. In line shutil.move(f"./IMPORT/"+file,f"./IMPORTED/"+file)

file is still open, there is some way to close it and move it in other folder?

Here is entire code:

    import os
    from pathlib import Path
    import shutil
    import ray
    import ray.util
    ray.init()
    import modin.pandas as pd
    
    current_directory = os.getcwd()
    import_folder_path = os.path.join(current_directory, 'IMPORT')
    folder_path: Path = Path(import_folder_path)
    file_list = []
    
    file_list = list(
        filter(lambda x: x if x.endswith('.xlsx') else None,
        os.listdir(folder_path))
    )
    df2 = []
    if len(file_list):
        excl_list=[]
        excl_merged = pd.DataFrame()
        imported_file_path = os.path.join(current_directory, 'IMPORTED\\')
        for file in file_list:
            file_path = os.path.join(folder_path,file)
            df=pd.read_excel(file_path)
            df = df[df['Delivery Status'] != 'Delivered']
            df2 = df.append(df)
            shutil.move(f"./IMPORT/"+file,f"./IMPORTED/"+file)
    
        output_file_path = os.path.join(folder_path,'output.xlsx')
        df2.to_excel(output_file_path, index=False)
    else:
        print("No excel file found")

Thank you for your help

1

There are 1 best solutions below

5
Tim Roberts On

There is a mention of this problem in https://github.com/pandas-dev/pandas/issues/29803. The suggested workaround is to manage the file handle lifetime yourself:

...
        for file in file_list:
            file_path = os.path.join(folder_path,file)
            with open(file_path,"rb") as xlfile:
                df=pd.read_excel(xlfile)

Pandas can read from a file handle, and this way the with ensures the handle is closed.