2017-07-19 183 views
0

我需要使用Python读取/下载用户提供的文件,但它给了我语法错误。尝试使用Python下载文件时出现语法错误

错误:

File "path1.py", line 6 return HttpResponse(content=test_file, content_type="text/plain") SyntaxError: 'return' outside function

我解释下面我的代码。

import os 
from django.http import HttpResponse 
#image_name = request.GET.get('param') 
image_name = "63b6ac1bc61642f39c697585810aed17.txt" 
test_file = os.path.join(os.getcwd(), image_name) 
return HttpResponse(content=test_file, content_type="text/plain") 

其实这里我的要求是如果我提供的文件名,需要读取/下载这个文件使用Python。

+0

删除return语句。你只能在函数内部返回。 –

回答

0

该代码确切地说是什么问题。您可以使用return statemmnt外部函数。你需要定义一个能够返回任何东西。

0

错误告诉你所有你想要的。你没有使用函数,但你正在返回一些东西。如果你不打电话,你怎么能回来?

试试这个:

import os 
from django.http import HttpResponse 
def downloadFile(): 
    #image_name = request.GET.get('param') 
    image_name = "63b6ac1bc61642f39c697585810aed17.txt" 
    test_file = os.path.join(os.getcwd(), image_name) 
    return HttpResponse(content=test_file, content_type="text/plain") 

downloadFile() 
+0

不起作用。它是Python,而不是JS;) – gonczor

+0

你赢得我的编辑8秒:P –

+0

快速或死亡:D – gonczor

相关问题