如何使用python 3.5在Google App Engine中打开文件?

时间:2019-01-08 08:31:06

标签: python-3.x google-app-engine

我可以使用本地计算机上的以下行加载txt文件。

lines=open(args['train_file1'],mode='r').read().split('\n')

args是具有训练文件目录的dict。

现在,我将工作的python版本更改为3.5,现在出现此错误。我不知道为什么会出现此错误,该目录中存在该文件。

enter image description here

FileNotFoundError: [Errno 2] No such file or directory: 'gs://bot_chat-227711/data/movie_lines.txt'

1 个答案:

答案 0 :(得分:2)

If I understood your question correctly, you are trying to read a file from Cloud Storage in App Engine.

You cannot do so directly by using the open function, as files in Cloud Storage are located in Buckets in the Cloud. Since you are using Python 3.5, you can use the Python Client library for GCS以便处理位于GCS中的文件。

这是一个小示例,它在App Engine应用程序的处理程序中读取存储桶中的文件:

from flask import Flask
from google.cloud import storage


app = Flask(__name__)


@app.route('/openFile')
def openFile():
    client = storage.Client()
    bucket = client.get_bucket('bot_chat-227711')
    blob = bucket.get_blob('data/movie_lines.txt')
    your_file_contents = blob.download_as_string()
    return your_file_contents

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=8080, debug=True)

请注意,您需要将google-cloud-storage行添加到requirements.txt文件中,以便导入和使用此库。