将函数转换为单行列表解析的麻烦

时间:2015-11-08 22:21:54

标签: python functional-programming

有人可以向我解释将此功能转换为单行列表理解的正确解决方案吗?

while(s.hasNextInt()){
    int temp = s.nextInt();
    inString += temp;
}

我已尝试过此功能,但它似乎无法正常工作(NameError,名称'文件'未定义)

def get_files(directory):
    filepaths = []
    for root, dirs, files in os.walk(directory):
        for filename in files:
           filepaths.append(os.path.join(root, filename))
    return filepath

1 个答案:

答案 0 :(得分:2)

for语句的顺序错误。尝试:

[os.path.join(root, filename) for root, dirs, files in os.walk(directory)
                              for filename in files]