0

The file contains information, I understand that I need to open the file and read its contents, maybe open it in a dictionary?

from flask import Flask, request

app = Flask(__name__)


@app.route('/')
def read_file():
    text = open(r'/Users/dev/Desktop/new_project/requiremets.txt')
    text.readline()
    return text


if __name__ == '__main__':
    app.run(debug=True)
DeF-F
  • 13
  • 1
  • You want to set the response type as in https://stackoverflow.com/questions/11773348/python-flask-how-to-set-content-type except that instead of "text/xml" I think it would be "application/text" – tdelaney Apr 24 '20 at 22:30
  • You can try `return text.read()` – Pedro Lobito Apr 24 '20 at 22:40

1 Answers1

0

You should writhe the following for reading a file:

f = open("text.txt","r") # 'r' for read, 'w' for write, if binaries add a b to one of those

To acces the file you then write:

f.read()

and then you can return it in your function. It'd look like this:

def read_file('/'):
    f = open("text.txt","r")
    return f.read()

Note: Use readlines() instead of read() to return a list with the content of the lines.

martin
  • 887
  • 7
  • 23