import os import shutil if os.path.exists('testingdir'): shutil.rmtree('testingdir') os.mkdir('testingdir') component0 = "a" path0 = "testingdir" path1 = path0 + "/" + component0 path0 = path1 + "/" + component0 path1 = path0 + "/" + component0 path0 = "testingdir" path2 = path1 + "/" + component0 os.makedirs(path2) result = [p for p in os.walk(path2,topdown=True,followlinks=True)] print result try: os.rename(path0,path2) except Exception as e: print "FAILED, RAISED",e pass result = [p for p in os.walk(path2,topdown=True,followlinks=True)] print result