Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python How to use extended path length

I am running my python code on Windows and trying to traverse and store all the file name with their paths in a file. But the Windows has a restriction of 260 characters.

os.chdir(self.config.Root_Directory_Path())        
    for root, dirs, files in os.walk("."):
        file_list.extend( join(root,f) for f in files )
    file_name_sorted = sorted(file_list)
    #file_sorted = sorted(file_list, key=getsize)
    #time.strftime("%m/%d/%Y %I:%M:%S %p" ,time.localtime(os.path.getmtime(file)))
    f = open(self.config.Client_Local_Status(),'wb')        
    for file_name in file_name_sorted:
        if (os.path.exists(file_name)):
            #f.write((str(os.path.getmtime(file_name)) + "|" + file_name + "\n").encode('utf-8'))
            pass
        else:
            print(file_name + "|" + str(len(file_name) + len(originalPath)) + "\n")
            print(os.path.getmtime(file_name))
            #f.write((str(os.path.getmtime(file_name)) + "|" + file_name + "\n").encode('utf-8'))
    f.close()

Because of the error, os.path.getmtime(file_name) throws an exception file not found. How can I overcome this problem? I tried using //?/ character as prefix, as suggested in

http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx

But was not successful in using //?/ character.

I tried using os.path.getmtime("////?//" + file_name) #Threw an error invalid path

Please suggest a fix

like image 638
Romaan Avatar asked Nov 16 '12 23:11

Romaan


People also ask

How do I enable path length limit in Python?

On the first screen, enable the “Add Python 3.6 to PATH” option and then click “Install Now.” Next, you have a decision to make. Clicking the “Disable path length limit” option removes the limitation on the MAX_PATH variable. This change won't break anything, but will allow Python to use long path names.

Is it good to disable path length limit in Python?

Python will work fine even the path length is disabled. Disabling the path length is better than shortening the path or the file name. Because if someone installed the Python in a directory more than a recommended length means it will cause the error. So using this option is much better.


1 Answers

The problem here is that you're using a relative path. The \\?\ prefix can only be applied to absolute paths. As the documentation says:

These prefixes are not used as part of the path itself. They indicate that the path should be passed to the system with minimal modification, which means that you cannot use forward slashes to represent path separators, or a period to represent the current directory, or double dots to represent the parent directory. Because you cannot use the "\\?\" prefix with a relative path, relative paths are always limited to a total of MAX_PATH characters.

The fix is simple. Instead of this:

'\\\\?\\' + file_name

do this:

'\\\\?\\' + os.path.abspath(file_name)

You cannot use forward slashes. It may or may not be legal to add an extra backslash, in which case you can get away with r'\\?\\' instead of doubling the double backslash. Try it and see (but make sure to test both drive-prefixed paths like C:\foo and UNC paths like \\server\share\bar)… But the doubled-backslash version above should definitely work.

like image 128
abarnert Avatar answered Nov 02 '22 05:11

abarnert