from __future__ import print_function import os import ctypes import ctypes.util libname = ctypes.util.find_library('c') libc = ctypes.cdll.LoadLibrary(libname) _getcwd = libc.getcwd _getcwd.argtypes = (ctypes.c_char_p, ctypes.c_size_t) _getcwd.restype = ctypes.c_char_p _strlen = libc.strlen _strlen.argtypes = (ctypes.c_char_p,) _strlen.restype = ctypes.c_size_t _buflen = 5000 _buffer = ctypes.create_string_buffer(_buflen) start = os.getcwd() subdirs = [] try: s = 'a' * 1 while True: os.mkdir(s) subdirs.append(os.path.join(os.getcwd(), s)) os.chdir(s) # ret = _getcwd(_buffer, _buflen) # if ret: # print("dirlen", _strlen(_buffer)) cwd = os.getcwd() print("cwd:", len(cwd), "...%s" % cwd[-10:]) finally: os.chdir(start) for s in reversed(subdirs): os.chdir(os.path.dirname(s)) os.rmdir(os.path.basename(s))