用python脚本为windows 7使用路径扩展\\?\ [英] Using path extension \\?\ for windows 7 with python script

查看:233
本文介绍了用python脚本为windows 7使用路径扩展\\?\的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ac2git 工具将我的Accurev仓库转换为git仓库。当python文件中的os.walk()函数运行时,我遇到了一个问题。由于我的项目有一个非常复杂的构建路径,我嵌套的文件路径长度超过了Windows 7的限制260.我尝试使用 microsoft support ,但它不能解决错误。我仍然得到错误[Winerror 3]:文件未找到,实际上它存在但由于长度限制无法访问。



这是ac2git.py脚本中的代码:

  def PreserveEmptyDirs(self):
preservedDirs = []
为os.walk中的根,目录,文件(self.gitRepo.path,topdown = True):
用于目录中的名称:

path =\\\\\ \\?\\+ ToUnixPath(os.path.join(root,name))


#保留不在.git /目录下的空目录。
如果git.GetGitDirPrefix(path)是None并且len(os.listdir(path))== 0:
filename = os.path.join(path,'.gitignore')
与codecs.open(文件名,'w','utf-8')作为文件:
#file.write('#accurev2git.py保留空dirs\\\
')
preservedDirs.append(文件名)
如果不是os.path.exists(文件名):
logger.error(无法保存目录,无法创建'{0}'。。format(filename))
return preservedDirs


def ToUnixPath(路径):
rv = SplitPath(路径)
如果rv不是无:
如果rv [0 ] =='/':
rv ='/'+'/'.join(rv [1:])
else:
rv ='/'.join(rv)
返回rv

def SplitPath(路径):
rv =无
如果路径不是无:
path = str(路径)
rv = []
drive,path = os.path.splitdrive(path)
head,tail = os .path.split(路径)
while len(head)> 0和head!='/'和head!='\\':#对于绝对路径,不会从头部删除起始斜线。
rv.append(tail)
head,tail = os.path.split(head)
if len(tail)> 0:
rv.append(tail)
if len(head)> 0:#对于绝对路径。
rv.append(head)
if len(drive)> 0:
rv.append(drive)
rv.reverse()
return rv

我附加了\\?\以允许更长的路径长度,但现在我得到这个错误:

<$ p $系统找不到指定的路径:'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' / code>

我是Python的新手,我不太确定解决这个问题的正确方法是什么。我必须继续使用Windows 7。任何建议,如果这个问题可以通过另一种方式解决吗?

解决方案

/ p>

显然这些信息非常重要在Windows API中,文件I / O函数将/转换为\,作为将名称转换为NT风格的名称,除了使用前面详细介绍的\?\前缀。



所以我刚添加了这段代码到函数:

  def ToUnixPath(路径):
rv = SplitPath(路径)
rv [ :] = [rv中item的项目!='/']
rv ='\\'.join(rv)
return r\\?+\\ \\\+ rv

它工作正常!


I'm using the tool 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 but it is not resolving the error. I still get the error [Winerror 3]: File not found , when in fact it is present but cannot be accessed due to the length limitation.

This is a part of the code in the ac2git.py script:

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

I have appended the "\\?\" in order to allow for longer path lengths but now I get this error:

FileNotFoundError: [WinError 3] The system cannot find the path specified: '\\\\?\\C:///s/cms'

I'm new to Python and I'm not very sure what is the right approach to tackle it. I have to continue using Windows 7 only. Any suggestions if this problem can be fixed another way?

解决方案

So after much ado, I made changes in the python code,

Apparently this information is very important " File I/O functions in the Windows API convert "/" to "\" as part of converting the name to an NT-style name, except when using the "\?\" prefix as detailed in the following sections."

So I just added this code to the function:

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

And it worked!

这篇关于用python脚本为windows 7使用路径扩展\\?\的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆