2

My project structure looks like

flask-appengine-template/
                        docs/
                        licenses/
                        src/
                            application/
                                        static/
                                        templates/
                                        models.py
                                        settings.py
                                        urls.py
                                        views.py
                                        english.txt
                        libs/
                            bs4/
                         app.yaml
                         src.py

in my views.py, I have a function that reads the file english.txt

      for words in open('english.txt', 'r').readlines():
            stopwords.append(words.strip())

When I run this on local environment, I see error in logs as

(<type 'exceptions.IOError'>, IOError(13, 'file not accessible'), <traceback object at 0x10c457560>)

How do I read this file in Google App Engine?

Lipis
  • 21,388
  • 20
  • 94
  • 121
daydreamer
  • 87,243
  • 191
  • 450
  • 722

2 Answers2

6

If english.txt is just a list of words, I suggest converting the list of words to a python list, so you can just import it.

If english.txt has more complex data, move it to bigtable or other database available to your app.

AppEngine is a very crippled environment compared to a standard VPS, I tend to avoid functions that operates over the underlying OS like open().

Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
  • I tried the way you suggested and it worked, Thank you @Paulo – daydreamer Feb 03 '13 at 16:00
  • @Paulo By _crippled_, you mean performance-wise or w.r.t. api support? When it comes to reading off the filesystem, don't templating libraries like `jinja` do just that all the time? – S B Oct 21 '17 at 06:59
  • This answer is quite old and somewhat outdated; the former "Platform as a Service" offer from GAE had several limitations compared to a VPS (for example, only pure Python libraries, limited I/O, etc). Nowadays I think they allow a kind of hybrid PaaS/IaaS approach where you can attach a network volume from GCP and do standard I/O in GAE. – Paulo Scardine Oct 23 '17 at 11:27
3

You'll need to use os.path to get the proper reference to the file path, something along the lines of:

def read_words():
    import os.path
    folder = os.path.dirname(os.path.realpath(__file__))
    file_path = os.path.join(folder, 'english.txt')
    for words in open(file_path, 'r').readlines():
        stopwords.append(words.strip())

Hope that helps!

kamalgill
  • 111
  • 2
  • 4