问题
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?
回答1:
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!
来源:https://stackoverflow.com/questions/39274722/using-path-extension-for-windows-7-with-python-script