python,return的位置和filesize

use*_*949 2 python return

函数在python中返回master的位置.

def myPath():
    for root,dirs,files in os.walk(dir):
        for fn in files:
            path = os.path.join(root, fn)
            return path
        return path
    return path
Run Code Online (Sandbox Code Playgroud)
ls /home/bb/C/
a.out  main.c  simple_write  t.c
Run Code Online (Sandbox Code Playgroud)

三个位置"回归"不是我想要的.

我想得到结果"C中的所有文件"

def filesize(path):
    for root, dirs, files in os.walk(PATH):
        for fn in files:
            path = os.path.join(root, fn)
            size = os.stat(path).st_size
            yield size,path

for size,path in filesize(PATH):
        print size,path
Run Code Online (Sandbox Code Playgroud)

但是,如何用以下代码实现上述功能?如何修改?


def find(path):
    return [os.path.join(root,fn)
        for root,dir,files in os.walk(dirs)
        for fn in files]
Run Code Online (Sandbox Code Playgroud)

Sve*_*ach 5

返回路径列表,而不仅仅是单个路径:

def find(path):
    return [os.path.join(root, fn)
            for root, dirs, files in os.walk(path)
            for fn in files]
Run Code Online (Sandbox Code Playgroud)

您也可以yield在内部循环内部使用获取生成器函数,请参阅Python yield关键字.