尝试使用Python下载文件时出现语法错误
我需要读取/下载用户使用Python提供的文件,但它给了我语法错误 错误: 文件“path1.py”,第6行 返回HttpResponse(content=test\u文件,content\u type=“text/plain”) SyntaxError:函数外部的“return” 我的代码如下:尝试使用Python下载文件时出现语法错误,python,Python,我需要读取/下载用户使用Python提供的文件,但它给了我语法错误 错误: 文件“path1.py”,第6行 返回HttpResponse(content=test\u文件,content\u type=“text/plain”) SyntaxError:函数外部的“return” 我的代码如下: import os from django.http import HttpResponse #image_name = request.GET.get('param') image_name = "
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读取/下载此文件。代码准确地说明了问题所在。在函数外部使用
return
statemmnt。您需要定义一个,以便能够返回任何内容。错误告诉您所需的一切。您没有使用函数,但返回了一些内容。如果你不打电话,你怎么能退货
试试这个:
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()
删除return语句。您只能在函数内部返回。要么快速返回,要么死掉:D@VíctorLópez:错误消失了,但它既不读取也不下载文件。