标签:blog color os 文件 for re c div
1.计算阶乘
通常你是这样写:
def myfunc(n): i = 1 if n>1: i = n return n*myfunc(n-1) return n
当然也可以这么写:
reduce (lambda x,y:x*y,range(1,n)) #简单多了?高大上了?
2.写(读)文件:
f=open(name,‘w‘) f.write(data) f.close()
也可以这样:
with open(name,‘w‘) as f: f.write(data) #不用关闭文件!
3.python 的私有:
想私有方法或者私有变量:方法或变量名前加‘_‘: def _myfunc() _color = ‘‘
4.遍历目录:
import os.path def Vist(arg,dirname,names): for filepath in names: print os.path.join(dirname,filepath) path =‘xxxx‘ 目录路径 os.path.walk(path,Vist,()) #Vist 回调函数 先遍历顶级目录,在遍历目录中文件
标签:blog color os 文件 for re c div
原文地址:http://www.cnblogs.com/huangxiaohen/p/3866445.html