我使用AWS EC2运行我的应用程序“艺术中的神经网络”。要发送图像,我部署了一个基于flask + virtualenv + apache的网站。在服务器上发送图像后启动脚本,打印每次迭代,我想将其存储在out.txt
__init__.py:
#...
def apply_style(image_name, style_name, iterations):
command = ['"python ~/neural_artistic_style/neural_artistic_style.py', \
' --subject ', '/var/www/superapp/superapp/uploads/' + image_name, \
' --style ', '/var/www/superapp/superapp/uploads/' + style_name, \
' --iterations ', iterations, \
' --network ~/neural_artistic_style/imagenet-vgg-verydeep-19.mat"']
str = ''.join(command)
network = Popen(str, shell=True, stdout=PIPE)
stats = []
for out in network.stdout:
stats.append(out)
line = ' '.join(stats)
return line
@app.route('/upload', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
image = request.files['image']
style = request.files['style']
iterations = request.form['iter']
if file and style:
#Saving images...
result = apply_style(image_name, style_name, iterations)
f = open('out.txt', 'w')
f.write(result)
f.close()
return 'FINISHED'
#...
好吧,我可以通过HTTP上传图片而无需在out.txt中写入结果,但是当我这样做时,apache日志会显示:
[Wed Jun 01 ...] [:error] [pid 2360:tid 14...] return self.view_functions[rule.endpoint](**req.view_args)
[Wed Jun 01 ...] [:error] [pid 2360:tid 14...] File "/var/www/superapp/superapp/__init__.py", line 72, in upload
[Wed Jun 01 ...] [:error] [pid 2360:tid 14...] f = open('out.txt', 'w')
[Wed Jun 01 ...] [:error] [pid 2360:tid 14...] IOError: [Errno 13] Permission denied: 'out.txt'
此目录中的所有文件现在都拥有777权限。当我尝试使用像script.py:
f = open('out.txt', 'w')
f.write('some words')
f.close()
永远的作品。但是用apache它没有。有没有人有任何想法如何解决它?
答案 0 :(得分:0)
问题是apache运行你的代码,其中有一个不同的工作目录,你不打算将文件写入。您必须提供该文件的完整路径。
f = open('/path/to/my/out.txt', 'w')