简体   繁体   中英

How to open a file in Google App Engine using python 3.5?

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 is dict which has the dir of training file.

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'

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

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM