当屈服和递归同时发生时,我很难理解
我想用我的代码遍历文件目录:import os
def doc_iter(fpath):
if os.path.isdir(fpath):
for child in os.listdir(fpath):
child=os.path.join(fpath,child)
print "this is ",child
for cn in doc_iter(child):
print "i am here1"
yield cn
print "yiedl1",cn
else:
print "i am here2"
yield fpath
print "yield2",fpath
有一个目录test,其中有三个子目录test1,test2,test3
在目录test1中,有两个文件test11,test12
在目录test2中,有两个文件test21,test22
在目录test3中,有两个文件test31,test32
^{pr2}$
结果让我很恼火,
1.产量1的印刷品等于产量2的印刷品?
2.至少要运行两个yield语句,在教科书中是这样说的when run into yield ,the program will be halt, the next next() make it continue??
3.让我们分析一下first next()的输出,为什么在i am here2"
4.what is the function of之后有两个i am here1s?
5.如何绘制详细的计算树?
6.如果你写一个函数来遍历目录bottom=[]
import os
def doc_iter(fpath):
if os.path.isdir(fpath):
for child in os.listdir(fpath):
child=os.path.join(fpath,child)
doc_iter(child)
else:
bottom.append(fpath)
return bottom
输出为:doc_iter("/home/debian/test")
['/home/debian/test/test2/test22','/home/debian/test2/test21','/home/debian/test/test','/home/debian/test3/test32','/home/debian/test/test3/test31','/home/debian/test/test1/test11','/home/debian/test/test1/test11','/home/debian/test/test1/test12']
函数和迭代器是不同的,
在函数中:doc_iter(child)
在迭代器中:for cn in doc_iter(child):
yield
这个例子有多复杂啊!在