使用路径扩展\\?\对于带有python scrip的windows 7

2024-04-26 06:39:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用工具ac2git将Accurev仓库转换为git仓库。我正面临一个问题os.步行()函数在python文件中运行。因为我的项目有一个非常复杂的构建路径,所以在Windows7上我有路径长度超过260限制的嵌套文件。我尝试使用microsoft support提供的解决方法,但它无法解决错误。我仍然得到错误[winerror3]:找不到文件,实际上它存在,但由于长度限制无法访问。你知道吗

这是ac2中代码的一部分吉特.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新手,我不太确定什么是解决它的正确方法。我只能继续使用Windows7。如果这个问题可以用另一种方法解决,有什么建议吗?你知道吗


Tags: 文件pathgit路径nonelenifos
1条回答
网友
1楼 · 发布于 2024-04-26 06:39:38

所以在做了很多ado之后,我修改了python代码

显然,这个信息非常重要“Windows API中的文件I/O函数convert”/”to“\”作为将名称转换为NT样式名称的一部分,但使用“\”时除外?\“前缀,详见以下章节。

所以我在函数中添加了以下代码:

def ToUnixPath(path):
rv = SplitPath(path)
rv[:] = [item for item in rv if item != '/']
     rv = '\\'.join(rv)
     return r"\\?"+"\\"+rv

成功了!你知道吗

相关问题 更多 >