2

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.

enter image description here

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

1 Answers1

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 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.

Joan Grau Noël
  • 3,084
  • 12
  • 21
  • Since OP wants to do some processing on the file, it's better to load it into memory rather than save to disk. For ways to load GCS file into memory check this: https://stackoverflow.com/questions/49357352/read-csv-from-google-cloud-storage-to-pandas-dataframe/50201179#50201179 – Lukasz Tracewski Jan 08 '19 at 12:42
  • I believe that the `download_as_string` method already does this, since it returns the content of the Blob file as a string of bytes, instead of saving it to a local file. I will edit the example to make it clearer. – Joan Grau Noël Jan 08 '19 at 12:57