Python语法点滴
1.判断字符为空
if not model_path:
2.显示当前脚本所在目录
import os cur_path = os.path.dirname(os.path.realpath(__file__)) print(cur_path)
3.显示当前被调用脚本所在目录
import os def getCurPath2(): cur_path = os.getcwd() return cur_path
4.将文件名和路径分割开
import os model_path,_ = os.path.split(os.path.realpath(__file__)) print(os.path.realpath(__file__)) # 路径 + 文件名 print(model_path) # 路径
5.组合路径,自动补全中间连接的“/”
import os model_path,_ = os.path.split(os.path.realpath(__file__)) model_det1_path = os.path.join(model_path, 'det1.npy') print(model_det1_path)
6.lambda定义函数
func=lambda x:x+1 print(func(1)) # 2