如何使用python 3.5在Google App Engine中打开文件? [英] How to open a file in Google App Engine using python 3.5?

查看:97
本文介绍了如何使用python 3.5在Google App Engine中打开文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am able to load my txt file using the line below on my local machine.

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

args是具有培训文件目录的dict.

args is dict which has the dir of training file.

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

Now i changed the working python version to 3.5 and now i am getting this error. I am clueless why this error is coming, the file is present in that directory.

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

推荐答案

如果我正确理解了您的问题,则您正在尝试从App Engine中的Cloud Storage中读取文件.

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

您不能直接使用open函数来执行此操作,因为Cloud Storage中的文件位于Cloud的存储桶中.由于您使用的是Python 3.5,因此可以使用 Python客户端库用于GCS ,以便处理位于GCS中的文件.

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 in order to work with files located in GCS .

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

This is a small example, that reads your file located in your Bucket, in a handler on an App Engine application:

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文件中,以便导入和使用此库.

Note that you will need to add the line google-cloud-storage to your requirements.txt file in order to import and use this library.

这篇关于如何使用python 3.5在Google App Engine中打开文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆