user3830867
user3830867

Reputation: 3

Python shutil copy failing

I have a python script running on windows that just copies contents of a directory from one location to other but am running into following error,not sure why ,i can confirm the source file is present,any idea on what could be wrong here?

 File "C:\crmapps\apps\python275-64\lib\shutil.py", line 208, in copytree
    raise Error, errors
shutil.Error: [('\\\\WPLBD04\\pkg\\builds\\promote\\2712\\2712_QCA1990ANFC_CNSS.HW_SP.2.0_win_pro\\sumatraservices\\inRexW\\TLM-2009-07-15\\docs\\doxygen\\html\\classtlm__utils_1_1instance__specific__extensions__per__accessor-members.html', '\\\\sun\\sscn_dev_integration\\promote_per_CL_artifacts\\TECH_PRO.CNSS.2.0\\20141013125710_1115240\\2712_QCA1990ANFC_CNSS.HW_SP.2.0_win_pro\\sumatraservices\\inRexW\\TLM-2009-07-15\\docs\\doxygen\\html\\classtlm__utils_1_1instance__specific__extensions__per__accessor-members.html', "[Errno 2] No such file or directory:

Upvotes: 0

Views: 3752

Answers (2)

wenzul
wenzul

Reputation: 4058

Like Lukas Graf said. The problem is that your destination path seems to be 266 characters long and therefore exceeds the limit.

The destination path is longer. So the error always will be in the destination because your source already exists. Assumed that your source path is not a extended-length path.

source: \\WPLBD04\pkg\builds\promote\2712\
destination: \\sun\sscn_dev_integration\promote_per_CL_artifacts\TECH_PRO.CNSS.2.0\20141013125710_1115240\
  1. You could try to shorten your filepath with win32api to may avoid this problem.

    string = win32api.GetShortPathName(path)

  2. You can prepend \\?\ to use extended-length paths.

>>> open(r"C:\%s\%s" % ("a"*1, "a"*254),"w")
<open file '...', mode 'w' at 0x0000000001F120C0>
>>> open(r"C:\%s\%s" % ("a"*2, "a"*254),"w")
IOError: [Errno 2] No such file or directory: '...'
>>> open(r"C:\%s\%s" % ("a"*1, "a"*255),"w")
IOError: [Errno 2] No such file or directory: '...'
>>> open(r"\\?\C:\%s\%s" % ("a"*1, "a"*255),"w")
<open file '\\\\?\\...', mode 'w' at 0x0000000001F12150>

Exaggerated: I don't think there is any noticable side effect on file access speed with extended-length paths. If you just want to avoid extended-length paths use a destination path which length is less than or equal to source path.

Upvotes: 0

tdelaney
tdelaney

Reputation: 77347

As mentioned, you have gone beyond the win32 path size limit. It turns out that the limit is in win32 and not the actual file system drivers. The trick to solving the problem is to prepend r"\\?\" to the path so that win32 will pass the paths along without mucking with them. It only works if you use absolute names including drive letter.

def win32_fix_long_path(path):
    return r'\\?\' + os.path.realpath(path)

It likely won't work in all situations, especially if you try to pass the name to a subprocess.

Upvotes: 2

Related Questions