I have a Flask app where I need to store temporary files for different instances of the app inside a folder whose name is the time at which the app was opened. I have an index function as follows:
@app.route('/')
def index():
session["now"] = time.time() * 100.0
os.mkdir(str(session["now"]))
The above code makes a directory where the name of the directory is the the time when the app was opened. When I open the app on two separate tabs on Chrome, two different directories are made as expected.
However, there is one issue. I have a text area in my UI whose text needs to be saved in a text file in the directory created above. I have used a POST request to send the data. The following is the Flask-Python method:
@app.route('/rt3/', methods=['POST'])
def fn3():
x = request.get_json()
data = x['textbox1']
f = open("./" + str(session["now"]) + "/mytext.txt", "w")
f.write(data)
f.close()
The issue is this: No matter on which Chrome tab I click the "run" button on, the text file always gets saved in the directory of the session created later. Hence the same text file is getting overwritten again and again. Since I have used session variables, shouldn't it save in the directory of its own session? Why is session variable getting overwritten?