0

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?

  • 3
    Random guess, it could be that after copying, the file path would be too long for Windows to handle. – hostingutilities.com May 02 '22 at 12:37
  • You could alias a drive name for a directory using [`subst`](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/subst) – Peter Wood May 02 '22 at 12:45
  • Not sure about path length being the issue... The same line of code worked tens of times before to copy files to a destination with the same path length. I will try a shorter path though... – Borja Garcia May 02 '22 at 19:52
  • After trying `subst` to shorten the source path and setting a shorter destination path, the same error is thrown: `shutil.Error: [('J:\\S2B_MSIL2A_20211023T111049_N0301_R137_T29TPG_20211023T130622.SAFE\\GRANULE\\L2A_T29TPG_A024187_20211023T111155\\IMG_DATA\\R60m', 'D:\\IA\\GRANULE\\L2A_T29TPG_A024187_20211023T111155\\IMG_DATA\\R60m', "[WinError 267] Directory name is invalid: 'J:\\\\S2B_MSIL2A_20211023T111049_N0301_R137_T29TPG_20211023T130622.SAFE\\\\GRANULE\\\\L2A_T29TPG_A024187_20211023T111155\\\\IMG_DATA\\\\R60m'")]` – Borja Garcia May 03 '22 at 06:15

0 Answers0