【发布时间】:2020-01-30 18:04:54
【问题描述】:
大家!
我确实需要从与我的 main.py 文件位于同一文件夹中的 json 文件中获取内容。问题是:Google Cloud Function 无法运行open() 语句。出现如下错误:
没有这样的文件或目录:'mytest.json'
现在,我正在将我的文件上传到 Cloud Storage,但我希望它在 Cloud Source 中。 那么,如何从云源获取我的 json 文件内容?
这是我在 Cloud Source 中的代码结构:
.
├── main.py
└── requirements.txt
└── mytest.json
mytest.json
{
'test': 'Hello World!'
}
main.py:
import json
with open('mytest.json', 'r') as testJson:
testDict = json.loads(testJson.read())
print(testDict['test'])
【问题讨论】:
-
您应该能够读取您使用函数部署的文件。请编辑问题以显示未按您期望的方式工作的实际代码。
-
你有错字吗?在您的问题中,您使用文件名
mytest.json,但在树中它显示myjson.json。 -
对不起,Doug,我不认为在这种情况下,代码很重要,因为它真的很简单。我刚刚尝试读取 json 和
open()行中出现的错误。
标签: python google-cloud-platform google-cloud-functions google-cloud-storage google-cloud-source-repos