Python - Windows maximum directory path length workaround

前端 未结 2 1292
终归单人心
终归单人心 2021-01-24 02:26

The problem is the character limit for the path in windows when creating multiple directories using pythons os.makedirs()

I found this post when searching f

2条回答
  •  臣服心动
    2021-01-24 02:26

    you need to use unc path and unicode filenames, but not all python functions are aware of this, os.mkdir works while os.makedirs not

    import os
    
    path = u'\\\\?\\c:\\'
    
    for i in xrange(1000):
        path += u'subdir\\'
        os.mkdir(path)
    

    but it's better to give also the code to remove them, windows explorer is unable to delete

    import os
    
    path = u'\\\\?\\c:\\'
    
    for i in xrange(1000, 0, -1):
        try:
            os.rmdir(path + (u'subdir\\' * i))
        except:
            pass
    

提交回复
热议问题