将路径扩展名 \\?\ 用于 windows 7 和 python 脚本
Using path extension \\?\ for windows 7 with python script
我正在使用工具 ac2git to convert my Accurev depot to git repository. I'm facing a problem when the os.walk() function in the python file runs. Since my project has a pretty complicated build path I have nested files that have path length exceeding the 260 limitation on Windows 7.I tried using the work-arounds provided by microsoft support,但它没有解决错误。我仍然收到错误 [Winerror 3]: File not found ,实际上它存在但由于长度限制而无法访问。
这是 ac2git.py 脚本中的部分代码:
def PreserveEmptyDirs(self):
preservedDirs = []
for root, dirs, files in os.walk(self.gitRepo.path, topdown=True):
for name in dirs:
path ="\\?\"+ToUnixPath(os.path.join(root, name))
# Preserve empty directories that are not under the .git/ directory.
if git.GetGitDirPrefix(path) is None and len(os.listdir(path)) == 0:
filename = os.path.join(path, '.gitignore')
with codecs.open(filename, 'w', 'utf-8') as file:
#file.write('# accurev2git.py preserve empty dirs\n')
preservedDirs.append(filename)
if not os.path.exists(filename):
logger.error("Failed to preserve directory. Couldn't create '{0}'.".format(filename))
return preservedDirs
def ToUnixPath(path):
rv = SplitPath(path)
if rv is not None:
if rv[0] == '/':
rv = '/' + '/'.join(rv[1:])
else:
rv = '/'.join(rv)
return rv
def SplitPath(path):
rv = None
if path is not None:
path = str(path)
rv = []
drive, path = os.path.splitdrive(path)
head, tail = os.path.split(path)
while len(head) > 0 and head != '/' and head != '\': # For an absolute path the starting slash isn't removed from head.
rv.append(tail)
head, tail = os.path.split(head)
if len(tail) > 0:
rv.append(tail)
if len(head) > 0: # For absolute paths.
rv.append(head)
if len(drive) > 0:
rv.append(drive)
rv.reverse()
return rv
我附加了“\\?\”以允许更长的路径长度,但现在我收到此错误:
FileNotFoundError: [WinError 3] The system cannot find the path specified: '\\?\C:///s/cms'
我是 Python 的新手,我不太确定解决它的正确方法是什么。我只能继续使用 Windows 7。如果这个问题可以通过其他方式解决,有什么建议吗?
所以费了九牛二虎之力,我对python代码进行了修改,
显然此信息非常重要" File I/O Windows API 中的函数将“/”转换为“\”作为转换名称的一部分到 NT 样式的名称,除非使用“\?\”前缀,如以下部分所述。"
所以我刚刚将这段代码添加到函数中:
def ToUnixPath(path):
rv = SplitPath(path)
rv[:] = [item for item in rv if item != '/']
rv = '\'.join(rv)
return r"\?"+"\"+rv
成功了!
我正在使用工具 ac2git to convert my Accurev depot to git repository. I'm facing a problem when the os.walk() function in the python file runs. Since my project has a pretty complicated build path I have nested files that have path length exceeding the 260 limitation on Windows 7.I tried using the work-arounds provided by microsoft support,但它没有解决错误。我仍然收到错误 [Winerror 3]: File not found ,实际上它存在但由于长度限制而无法访问。
这是 ac2git.py 脚本中的部分代码:
def PreserveEmptyDirs(self):
preservedDirs = []
for root, dirs, files in os.walk(self.gitRepo.path, topdown=True):
for name in dirs:
path ="\\?\"+ToUnixPath(os.path.join(root, name))
# Preserve empty directories that are not under the .git/ directory.
if git.GetGitDirPrefix(path) is None and len(os.listdir(path)) == 0:
filename = os.path.join(path, '.gitignore')
with codecs.open(filename, 'w', 'utf-8') as file:
#file.write('# accurev2git.py preserve empty dirs\n')
preservedDirs.append(filename)
if not os.path.exists(filename):
logger.error("Failed to preserve directory. Couldn't create '{0}'.".format(filename))
return preservedDirs
def ToUnixPath(path):
rv = SplitPath(path)
if rv is not None:
if rv[0] == '/':
rv = '/' + '/'.join(rv[1:])
else:
rv = '/'.join(rv)
return rv
def SplitPath(path):
rv = None
if path is not None:
path = str(path)
rv = []
drive, path = os.path.splitdrive(path)
head, tail = os.path.split(path)
while len(head) > 0 and head != '/' and head != '\': # For an absolute path the starting slash isn't removed from head.
rv.append(tail)
head, tail = os.path.split(head)
if len(tail) > 0:
rv.append(tail)
if len(head) > 0: # For absolute paths.
rv.append(head)
if len(drive) > 0:
rv.append(drive)
rv.reverse()
return rv
我附加了“\\?\”以允许更长的路径长度,但现在我收到此错误:
FileNotFoundError: [WinError 3] The system cannot find the path specified: '\\?\C:///s/cms'
我是 Python 的新手,我不太确定解决它的正确方法是什么。我只能继续使用 Windows 7。如果这个问题可以通过其他方式解决,有什么建议吗?
所以费了九牛二虎之力,我对python代码进行了修改,
显然此信息非常重要" File I/O Windows API 中的函数将“/”转换为“\”作为转换名称的一部分到 NT 样式的名称,除非使用“\?\”前缀,如以下部分所述。"
所以我刚刚将这段代码添加到函数中:
def ToUnixPath(path):
rv = SplitPath(path)
rv[:] = [item for item in rv if item != '/']
rv = '\'.join(rv)
return r"\?"+"\"+rv
成功了!