Python shutil copy error

Soldato
Joined
12 May 2014
Posts
5,304
Hi guys. I've made a script to copy folders and files and zip them. The problem is that the shutil.copy command fails to copy a file and I can't figure out why.

Here's the error message:
IOError: [Errno 2] No such file or directory: "E:\\University course\\University course_Archive\\Part C\\Semester 1 and 2\\Group project\\13TTC101 - Vehicle Concept Definition and Design\\CAD\\Vauxhall's CAD\\Cradle, Steering rack, Steering column and Front suspension\\Cradle_Steering_rack_Steering_column_and_Front_suspension.CATProduct"

Here's the function it fails on:

def copy_folder(source, dest):
(folder_head, folder_tail) = os.path.split(source)
os.mkdir(os.path.join(dest, folder_tail))
dest_folder = os.path.join(dest, folder_tail)
print "-----------------------------------------------"
print "This is the source: %s" %source
print "The folder tail is: %s" %folder_tail
print "The dest is: %s" %dest
print "This dest_folder_1: %s" %dest_folder
for folder_item in os.listdir(source):
print "This dest_folder_2: %s" %dest_folder
if os.path.isdir(os.path.join(source, folder_item)):
copy_folder(os.path.join(source, folder_item), dest_folder)
else:
if len(os.path.abspath(os.path.join(source, folder_item))) < 255:
pass
# shutil.copy2(os.path.join(source, folder_item), dest_folder)
else:
# print "Character limit has been reached"
# print "\\\\?\\{}".format(os.path.abspath(os.path.join(source, folder_item)))
#print dest_folder
print "This dest_folde_3r: %s" %dest_folder
r"{}".format(os.path.abspath(os.path.join(source, folder_item)))
shutil.copy2(r"{}".format(os.path.abspath(os.path.join(source, folder_item))), dest_folder)# r"{}".format(dest_folder))

Here's the last set of outputs from the code:

-----------------------------------------------
This is the source: E:\University course\Part C\Semester 1 and 2\Group project\13TTC101 - Vehicle Concept Definition and Design\CAD\Vauxhall's CAD\Cradle, Steering rack, Steering column and Front suspension\Cradle
The folder tail is: Cradle
The dest is: E:\University course\University course_Archive\Part C\Semester 1 and 2\Group project\13TTC101 - Vehicle Concept Definition and Design\CAD\Vauxhall's CAD\Cradle, Steering rack, Steering column and Front suspension
This dest_folder_1: E:\University course\University course_Archive\Part C\Semester 1 and 2\Group project\13TTC101 - Vehicle Concept Definition and Design\CAD\Vauxhall's CAD\Cradle, Steering rack, Steering column and Front suspension\Cradle
This dest_folder_2: E:\University course\University course_Archive\Part C\Semester 1 and 2\Group project\13TTC101 - Vehicle Concept Definition and Design\CAD\Vauxhall's CAD\Cradle, Steering rack, Steering column and Front suspension\Cradle
This dest_folder_2: E:\University course\University course_Archive\Part C\Semester 1 and 2\Group project\13TTC101 - Vehicle Concept Definition and Design\CAD\Vauxhall's CAD\Cradle, Steering rack, Steering column and Front suspension\Cradle
This dest_folder_2: E:\University course\University course_Archive\Part C\Semester 1 and 2\Group project\13TTC101 - Vehicle Concept Definition and Design\CAD\Vauxhall's CAD\Cradle, Steering rack, Steering column and Front suspension
This dest_folde_3r: E:\University course\University course_Archive\Part C\Semester 1 and 2\Group project\13TTC101 - Vehicle Concept Definition and Design\CAD\Vauxhall's CAD\Cradle, Steering rack, Steering column and Front suspension
E:\University course\Part C\Semester 1 and 2\Group project\13TTC101 - Vehicle Concept Definition and Design\CAD\Vauxhall's CAD\Cradle, Steering rack, Steering column and Front suspension\Cradle_Steering_rack_Steering_column_and_Front_suspension.CATProduct

Does anyone know what's causing this error?
 
Thanks. It turn out that I had to apply '\\?\\' to the destination folder as well.

Just for anyone that reads this. if your using shutil.rmtree with long path names you need to write the file path as u"\\\\?\\c:\long_file_path"
 
Back
Top Bottom