y33t
y33t

Reputation: 679

Python overcome Windows 255 path+filename limit and archive the directory

I will backup a directory located in a Windows 7 machine with Python. I need to compress it as much as possible. I haven't decided which way to go for compression yet.

The main problem is directory contains hundreds of files/directories inside as cascaded thus I can't see inside after total 255 characters of path+filename. This directory structure is created by a 3rd party software.

Just for a quick test, I wrote the following for folder generation (to simulate) ;

    start = time.time()
    end = time.time() - start
    root_dir = 'C:\test'
    os.chdir("C:\test")
    iterate = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    while (end<1):                
            end = time.time() - start   
            os.system("mkdir " + str(iterate))
            os.chdir(root_dir + "\\" + str(iterate))
            root_dir = os.getcwd() 

Results in this ;

    The filename or extension is too long.
    Traceback (most recent call last):
      File "folder_generate.py", line 21, in <module>
        os.chdir(root_dir + "\\" + str(iterate))
    WindowsError: [Error 2] The system cannot find the file specified: 'c:\\Python27
    \\ABCDEFGHIJKLMNOPQRSTUVWXYZ\\ABCDEFGHIJKLMNOPQRSTUVWXYZ\\ABCDEFGHIJKLMNOPQRSTUV
    WXYZ\\ABCDEFGHIJKLMNOPQRSTUVWXYZ\\ABCDEFGHIJKLMNOPQRSTUVWXYZ\\ABCDEFGHIJKLMNOPQR
    STUVWXYZ\\ABCDEFGHIJKLMNOPQRSTUVWXYZ\\ABCDEFGHIJKLMNOPQRSTUVWXYZ\\ABCDEFGHIJKLMN
    OPQRSTUVWXYZ'

As it was pointed out in another question here but not answered clearly.

I got a quick test code for walking through directory content ;

    fileList = []
    fileSize = 0
    folderCount = 0
    rootdir = "c:\\test"

    for root, subFolders, files in os.walk(rootdir):
        folderCount += len(subFolders)
        for file in files:
            f = os.path.join(root,file)
            fileSize = fileSize + os.path.getsize(f)
            fileList.append(f)

    print("Total Size is {0} bytes".format(fileSize))
    print("Total Files ", len(fileList))
    print("Total Folders", folderCount)  

Results in ;

     Total Size is 0 bytes
     ('Total Files ', 0)
     ('Total Folders', 9)

No matter how many folders are in the test folder. Funny thing is I can browse and create new folder after 9.folder through the explorer seamlessly.

I am no into wind0z that much thus I would appreciate if experienced users can help me get over this.

Just archive a very long directory.

Upvotes: 4

Views: 5453

Answers (1)

reuf
reuf

Reputation: 550

Limit can be overomce by using subst if that is the main problem.

Take a look at this:

How to unicode in Python (it talks about 255 limit) - http://docs.python.org/release/3.1.5/howto/unicode.html

http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx - pay attention to Unicode part...It explains that Unicode versions of Windows APIs have higher limits - also how to enable this.

Take a look at this as well: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/subst.mspx?mfr=true

Take a look at this discussion - http://forums.v3.co.uk/showthread.php?t=223343

Other than this, if work is what you have to get done and this is not for your own pure learning and amusement, I can tell you that there are far better solutions for backing up what you need than for you to create your own from scratch.

Upvotes: 3

Related Questions