Python shutil.copytree - WinError 267 Directory name is invalid

347 Views Asked by At

I am trying to copy a Sentinel 2 folder tree using the Python function shutil.copytree. I have handled paths with the os package to prevent errors derived from bad path names. However, when calling shutil.copytree it throws the following error:

Copying O:\Sentinel\2021\Mes_10\huso_29\tile_29TPG\S2B_MSIL2A_20211023T111049_N0301_R137_T29TPG_20211023T130622.SAFE\manifest.safe
Copying O:\Sentinel\2021\Mes_10\huso_29\tile_29TPG\S2B_MSIL2A_20211023T111049_N0301_R137_T29TPG_20211023T130622.SAFE\MTD_MSIL2A.xml
Copying O:\Sentinel\2021\Mes_10\huso_29\tile_29TPG\S2B_MSIL2A_20211023T111049_N0301_R137_T29TPG_20211023T130622.SAFE\rep_info\S2_PDI_Level-2A_Datastrip_Metadata.xsd
Copying O:\Sentinel\2021\Mes_10\huso_29\tile_29TPG\S2B_MSIL2A_20211023T111049_N0301_R137_T29TPG_20211023T130622.SAFE\rep_info\S2_PDI_Level-2A_Tile_Metadata.xsd
Copying O:\Sentinel\2021\Mes_10\huso_29\tile_29TPG\S2B_MSIL2A_20211023T111049_N0301_R137_T29TPG_20211023T130622.SAFE\rep_info\S2_User_Product_Level-2A_Metadata.xsd
Traceback (most recent call last):
  File "D:\Repositorios\Desarrollo\Super_Resolucion\testing\L3.py", line 109, in <module>
    main()
  File "D:\Repositorios\Desarrollo\Super_Resolucion\testing\L3.py", line 100, in main
    GenerateDataset(
  File "D:\Repositorios\Desarrollo\Super_Resolucion\testing\s2_dataset_supres.py", line 154, in GenerateDataset
    shutil.copytree(os.path.join(scenes_dir, scene), buffer_scene, dirs_exist_ok=True)
  File "C:\Users\Calabacin_fora\anaconda3\envs\SR\lib\shutil.py", line 565, in copytree
    return _copytree(entries=entries, src=src, dst=dst, symlinks=symlinks,
  File "C:\Users\Calabacin_fora\anaconda3\envs\SR\lib\shutil.py", line 521, in _copytree
    raise Error(errors)
shutil.Error: [('O:\\Sentinel\\2021\\Mes_10\\huso_29\\tile_29TPG\\S2B_MSIL2A_20211023T111049_N0301_R137_T29TPG_20211023T130622.SAFE\\GRANULE\\L2A_T29TPG_A024187_20211023T111155\\IMG_DATA\\R60m', 'D:\\IA\\L3_TEST\\29TPG\\29TPG_202110\\temp\\scenes\\buffer\\S2B_MSIL2A_20211023T111049_N0301_R137_T29TPG_20211023T130622.SAFE\\GRANULE\\L2A_T29TPG_A024187_20211023T111155\\IMG_DATA\\R60m', "[WinError 267] Directory name is invalid: 'O:\\\\Sentinel\\\\2021\\\\Mes_10\\\\huso_29\\\\tile_29TPG\\\\S2B_MSIL2A_20211023T111049_N0301_R137_T29TPG_20211023T130622.SAFE\\\\GRANULE\\\\L2A_T29TPG_A024187_20211023T111155\\\\IMG_DATA\\\\R60m'")]

I know for sure the paths used exist and are folders, and have even used this function to print the copying progress, showing that the function copies just fine some of the files and sub-directories before crashing:

def copy2_verbose(src, dst):
        print('Copying {0}'.format(src))
        shutil.copy2(src,dst)

shutil.copytree(os.path.join(scenes_dir, scene), buffer_scene, dirs_exist_ok=True, copy_function=copy2_verbose)

Any idea what might be happening?

0

There are 0 best solutions below