from __future__ import print_function import os here = os.getcwd() subdirs = [] subdir = here while True: subdir = os.path.join(subdir, "aaaaab") subdirs.append(subdir) if len(subdir) > 1026: break try: for s in subdirs: os.mkdir(s) print("mkdir:", len(s)) os.chdir(subdirs[-1]) print("cwd:", len(os.getcwd()), "...%s" % os.getcwd()[-10:]) finally: os.chdir(here) for s in reversed(subdirs): try: os.rmdir(s) except EnvironmentError: pass