python3中的f字符串出现名称错误

时间:2018-07-18 12:22:07

标签: python python-3.x f-string

我在Web应用程序中使用以下f字符串。当我在本地运行它(在尝试上网之前)时,出现名称错误:

NameError:未定义名称“路径”

但是路径可以正确打印,并且似乎已经定义好了。我在做什么错了?

path = f'{PROJECT_PATH["raw_data"]}/cashValues'
print("PATH", path)
filenames = [f'{path}/{i}' for i in filenames]

谢谢

完整追溯:

Traceback (most recent call last):
  File "scripts/TableTransform.py", line 8, in <module>
    class TableTransform(object):
  File "scripts/TableTransform.py", line 14, in TableTransform
    Filenames = [f'{path}/{i}' for i in filenames]
  File "scripts/TableTransform.py", line 14, in <listcomp>
    Filenames = [f'{path}/{i}' for i in filenames]
NameError: name 'path' is not defined

1 个答案:

答案 0 :(得分:0)

似乎python 3.6.5中的列表理解具有自己的范围,并且不知道该范围之外的变量。这就是为什么它失败了。