pythonwindowsioshutilmax-path

Shutil.copy IO Error2 when directory exists


I'm encountering a troublesome problem with my code and I've been unable to figure it out. Basically I am copying files from a local directory on my computer to a Dropbox folder that acts as a project repository for me and some other folks.

I keep hitting an IO Error when executing the shutil.copy line. Errno 2, N osuch file or directory. However the directory and file both exist. When I change the directory to a different/test location (test_dir in my code), the code runs perfectly fine. Any insights would be greatly appreciated.

import os, os.path
import re
import shutil
import sys

#File Location
directory_list = "path where files are located"

#Dropbox base directory: 
dropbox = "path to dropbox directory"

test_dir = "path to test directory on my local machine"    

sed_files = os.listdir(directory_list)

for i in sed_files:
    #print i.split("BBB")[0]

    #df
    copy_dir = re.sub(r'XXX',r'_',i.split("BBB")[0])
    copy_dir = re.sub(r'ZZZ',r'/',copy_dir)
    copy_dir = dropbox + copy_dir + "/FIXED/"   
    if not os.path.exists(copy_dir):
        os.makedirs(copy_dir)       

    shutil.copy(directory_list+i,copy_dir)
    #print directory_list+i
    #os.rename(copy_dir+i,copy_dir+i.split("BBB")[1])

Traceback error is:

Traceback (most recent call last):
File "copy_SE_files.py", line 25, in <module> shutil.copy(direcotry_list+i,copydir)
File "C:\Python27\ArcGIS10.1\lib\shutil.py", line 116, in copy copyfile(src,dst)
File "C:\Python27\ArcGIS10.1\lib\shutil.py", line 82, in copyfile with open(dst, 'wb') as fdst:
IOError: [Errno 2] No such file or directory: 'C:/Users/myusername/Dropbox/NASA_HyspIRI_Project/Field_Data/Spectra/CVARS/April2014/LemonTrees/04172014_SE_LemonTreeCanopy/SE_Files/SpectraZZZCVARSZZZApril2014ZZZLemonTreesZZZZ04172014XXXSEXXXLemonTreeCanopyZZZSEXXXFilesBBBCVARS_na_LemonTrees_Bareground1_4deg_Refl_00355.sed'

Problem solved thanks to the keen eyes of stack overflow. Modified the line to read:

shutil.copy(directory_list+i,'\\\\?\\'+os.path.abspath(copy_dir))

Solution

  • You're failing because the combined length of the path is greater than Window's MAX_PATH limit. C:/Users/myusername/Dropbox/NASA_HyspIRI_Project/Field_Data/Spectra/CVARS/April2014/LemonTrees/04172014_SE_LemonTreeCanopy/SE_Files/SpectraZZZCVARSZZZApril2014ZZZLemonTreesZZZZ04172014XXXSEXXXLemonTreeCanopyZZZSEXXXFilesBBBCVARS_na_LemonTrees_Bareground1_4deg_Refl_00355.sed is 274 characters long, and without going to some trouble, most Windows file manipulation APIs won't work properly with a path longer than MAX_PATH (which is 260, and one of them is reserved for the NUL terminator).

    Assuming Python uses the correct APIs, you can make it work with the extended path prefix, \\?\ (and it may require you to use backslashes rather than forward slashes in your path; I'm not clear on that; read the docs).