有人可以向我解释将此功能转换为单行列表理解的正确解决方案吗?
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
答案 0 :(得分:2)
for
语句的顺序错误。尝试:
[os.path.join(root, filename) for root, dirs, files in os.walk(directory)
for filename in files]