我有FLASK应用程序 www / FlaskApp / FlaskApp / init .py文件带有功能 python文件下一个包含
@app.route('/')
def hello():
file = open('myfile.txt', 'w+')
os.mknod("newfile.txt")
return render_template('page2.html')
但是如果我运行网站,它的返回错误,在文件日志中写
PermissionError:[Errno 13]权限被拒绝:'myfile.txt'
为所有www目录设置permision 777 打开FileZilla 右击www dir,设置777 permision 为什么文件不创建?
答案 0 :(得分:0)
不确定这是否是一个最佳解决方案,而且我不太了解Flask,因为它告诉你为什么相对路径不起作用(我认为它会写出你的python脚本所在的文件)但是你可以通过使用环境变量指定存储应用数据的位置来使其工作。例如:
import os
@app.route('/')
def open_file():
filename = os.path.join(os.environ['FLASK_APP_DATA'], 'myfile.txt')
print (filename)
file = open(filename, 'w+')
file.write("This is a test")
file.close()
然后你可以在开发框和你的产品盒上设置不同的环境变量。