A very simple task, but fails to perform. You need to copy folder1 to the directory D: \ savorsk \

import shutil src = "D:\\Project\\2014\\archdata\\folder1" dst_sav = "D:\\savorsk\\" shutil.copytree(src, dst_sav) 

The specified code leads to an error. I read the documentation very thoughtfully, but I really don't catch up with what is wrong in the code.

 D:\123>1.py Traceback (most recent call last): File "D:\123\1.py", line 5, in <module> shutil.copytree(src, dst_sav) File "C:\Python27\ArcGIS10.2\lib\shutil.py", line 175, in copytree os.makedirs(dst) File "C:\Python27\ArcGIS10.2\lib\os.py", line 157, in makedirs mkdir(name, mode) WindowsError: [Error 183] โ•ั…ั‚ัŽั‡ัŒัŽั†ััŽ ั‘ัŽั‡ั„ั€ะ„โ„– ะ‡ั€ั‰ั‹,: 'D:\\savorsk' 
  • Do you have any mistake to show - LinnTroll
  • try this: shutil.copytree ('D: \\ Project \\ 2014 \\ archdata \\ folder1', 'D: \\ savorsk \\ folder1') - LinnTroll
  • Added it. As I understand, this thing is trying to create savorsk catalog, but I donโ€™t need it. I need to copy everything into it. - Dmitry Bubnenkov
  • I can not understand, but how can I be if my code variable paths are formed on the fly. I really do not understand the logic. - Dmitry Bubnenkov
  • Could show a code sample how to copy the necessary directory in the target. Just now I do not understand the logic. I just donโ€™t really understand how folder1 gets into dst_sav = "D: \\ savorsk \\" folder1 - Dmitry Bubnenkov

1 answer 1

Do you have any mistake

WindowsError: [Error 183] Cannot create a file when that already exists: 'D: \ savorsk'

As I understand, this thing is trying to create savorsk catalog, but I donโ€™t need it.

The problem is that if the target folder already exists on the disk, then shutil.copytree () always returns an error.

Could you show a sample code how to copy the desired directory to the target

 import os import shutil def my_copytree(src, dst, symlinks=False, ignore=None): for item in os.listdir(src): s = os.path.join(src, item) d = os.path.join(dst, item) if os.path.isdir(s): shutil.copytree(s, d, symlinks, ignore) else: shutil.copy2(s, d) if __name__ == "__main__": src = "D:\\Project\\2014\\archdata\\folder1" dst_sav = "D:\\savorsk\\" my_copytree(src, dst_sav)