三元表达式
语法:结果1 if 条件 else 结果2
运行过程:判断条件是否为真,如果条件为真返回结果1,否则返回结果2
a = 10
b = 20
ret = a if a>b else b
print(ret)
递归
一个函数在执行过程中一次或多次调用其本身
import os
# 实例:递归遍历一个文件夹
def read(path, ceng):
lst = os.listdir(path)
for name in lst:
# 拼接正确的文件路径
real_path = os.path.join(path, name)
if os.path.isdir(real_path):
# 是文件夹
print(real_path)
# 递归
read(real_path, ceng+1)
else:
# 普通文件
print("--|"*ceng, name)
# open(real_path, mode="w").write(1) # 往所有文件中写入1(病毒)
read("../python_quadrotor_simulator", 0)