Python - Windows maximum directory path length workaround

前端 未结 2 1311
终归单人心
终归单人心 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:29

    Per this stackoverflow answer: while chdir can go up one directory with os.chdir(".."), the platform-agnostic way is: os.chdir(os.pardir).

    Either call this N times in a loop;
    or try an unreadable one-liner like this (untested):
    os.chdir(os.path.join(*([os.pardir] * NUM_TIMES)))

    (Instead of path.split('/'), you could also use the method described here for it to work on all operating systems)

提交回复
热议问题