1

I'm trying to build a website using the Flask framework for Python.

I'm on a Linux Ubuntu server, with Apache2.

On my website, whenever someone enters the URL "/Elv_1.html", I want to open a .txt file, get some values and create a graph using pygal. Here is my code:

@app.route('/river_1.html')
def riv_1():
    try:
        document = open('temp.txt','r')

        temp_list = []
        for n in document:
            n = n.rstrip('\n')
            n = int(n)
            temp_list.append(n)

        document.close()

        graf = pygal.Line(title=u'Tempt last 24h')
        graf.x_labels = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24)
        graf.add('Temp', temp_list)
        graf = graf.render_data_uri()

        return render_template('river_1.html', graf=graf)
    except Exception, e:
        return str(e)

if __name__ == '__main__':
    app.run(debug=True)

The file 'temp.txt' is located in the same directory as the __init__.py file. __init__.py is the Flask app that the code comes from.

When I do this on my computer using localhost to run the server, it works just fine. However, when I upload this to my Linux server and try to enter that specific URL, it shows the following error:

[Error 2] No such file or directory: 'temp.txt'

Any suggestions as to why it doesn't appear to find the file?

aaron
  • 39,695
  • 6
  • 46
  • 102
jakvah
  • 11
  • 1
  • 1
  • 2
  • 1
    Does the file exist on the server too? That is, did you upload the `temp.txt` too to the server, not only the Python program? – janos Nov 25 '17 at 06:08
  • seems like the file is not in the same directory as flask file. see if that is the issue. these problems are usually found due to incorrect name or incorrect path – VarunJoshi129 Nov 25 '17 at 06:50
  • when you run your application on local, they are aside, but on server, you may call your app using `wsgi server`, (will, you should if you aren't!), there is a bit difference between python path there, and you shold change the way you give `path` to files. – senaps Nov 25 '17 at 09:09
  • Yes, the file is uploaded to the server. – jakvah Nov 25 '17 at 17:59
  • my __init__.py is in the same folder as temp.txt – jakvah Nov 25 '17 at 18:00

2 Answers2

2

Try using the os module when specifying the path to your file. I am asuming you are using a windows pc when runing on localhost?

import os
document_path = os.getcwd()+'temp.txt'
document = open(documnet_path, 'r')
Kristian
  • 36
  • 4
1

Make sure you are running the server from it's directory. So if you have this structure as shown below, you can't simply open terminal and type server/__init__.py, because you are in your home directory (/home/username/). You need to cd to server and there run ./__init__.py

/home/
  username/
    server/
      __init__.py
      temp.txt

Or, if you want to run it from somewhere else, run open the file from os.path.abspath(os.path.dirname(__file__)) + '/temp.txt') (Tested with python 3.5.2)
See python docs for os.path.

M. Volf
  • 1,259
  • 11
  • 29