#### 遍历目录里的文件(不支持子目录文件)
import os
for i in os.listdir(‘C:/Users/Administrator/Desktop/技术文档‘):
print (i)
###########################################
#### 目录是否存在
import os
d = os.path.isdir(‘C:/Users/Administrator/Desktop/技术文档/.‘)
print(d)
> True
#### 文件是否存在
import os
d = os.path.isfile(‘C:/Users/Administrator/Desktop/技术文档‘)
print(d)
> False
#### 路径连接
import os
d = os.path.join(‘C:/Users/Administrator/Desktop‘,‘aa‘,‘bb‘)
print(d)
> C:/Users/Administrator/Desktop\aa\bb
######################################################
#### 遍历目录里的文件和子目录里的文件
import os
def print_files(path):
lsdir = os.listdir(path)
dirs = [i for i in lsdir if os.path.isdir(os.path.join(path, i))]
files = [i for i in lsdir if os.path.isfile(os.path.join(path, i))]
if files:
for f in files:
print(os.path.join(path, f))
if dirs:
for d in dirs:
print_files(os.path.join(path, d))
print_files(‘C:/Users/Administrator/Desktop/技术文档‘)
defun(x, y):
return x*y
#
r = lambda x,y:x*y
print(r(2,3)) # 调用
> 6
from functools import reduce
def add(x, y):
return x + y
sum = reduce(add, range(1,101))
print(sum)
> 5050
#
from functools import reduce
print(reduce(lambda x,y:x+y, range(1,101)))
> 5050
原文:http://blog.51cto.com/13542406/2058850