I write Python script to copy files; unfortunately it keeps failing because filename is too long(>256). Is there anyway to deal with that problem?
I'm using Python 2.5.4 and Windows XP.
Cheers,
I write Python script to copy files; unfortunately it keeps failing because filename is too long(>256). Is there anyway to deal with that problem?
I'm using Python 2.5.4 and Windows XP.
Cheers,
In order to use the \\?\
prefix (as already proposed), you also need to make sure you use Unicode strings as filenames, not regular (byte) strings.
For anyone else looking for solution here:
\\?\
as already stated, and make sure string is unicode;You'll have to write something like:
def remove_dir(directory):
long_directory = '\\\\?\\' + directory
shutil.rmtree(long_directory, onerror=remove_readonly)
def remove_readonly(func, path, excinfo):
long_path = path
if os.sep == '\\' and '\\\\?\\' not in long_path:
long_path = '\\\\?\\' + long_path
os.chmod(long_path, stat.S_IWRITE)
func(long_path)
This is an example for Python 3.x so all strings are unicode.
This answer by uDev suggests to add
# Fix long path access:
import ntpath
ntpath.realpath = ntpath.abspath
It seems to work for me.
Another thing that works for me is to change directory into the place to which I want to copy:
import os
import shutil
def copyfile_long_path(src, dst):
src_abs = os.path.abspath(src)
dst_abs = os.path.abspath(dst)
cwd = os.getcwd()
os.chdir(os.path.dirname(dst))
shutil.copyfile(src_abs, os.path.filename(dst))
os.chdir(cwd)
if not os.path.isfile(dst_abs):
raise Exception("copying file failed")