获取TypeError:预期的str,字节或os.PathLike对象,而不是InMemoryUploadedFile [英] Getting a TypeError: expected str, bytes or os.PathLike object, not InMemoryUploadedFile

查看:96
本文介绍了获取TypeError:预期的str,字节或os.PathLike对象,而不是InMemoryUploadedFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def post(self, request):
    form = CsvUploadForm(request.POST, request.FILES)

    if form.is_valid():
        with open(request.FILES['csv']) as csv_source:
            has_header = csv.Sniffer().has_header(csv_source.read(1024))
            csv_source.seek(0)
            reader = csv.DictReader(csv_source)
            ....
            ....

    return HttpResponse("file Uploaded")

使用上述代码,我试图上传一个csv文件并阅读它.但我收到以下错误.

using the above code i am trying to upload a csv file and read it. but I am getting the following error.

以open(request.FILES ['csv'])作为文件:TypeError:预期的str,字节或os.PathLike对象,而不是InMemoryUploadedFile

with open(request.FILES['csv']) as file: TypeError: expected str, bytes or os.PathLike object, not InMemoryUploadedFile

我在这里想念什么?

Django 2.0.7python 3.5

django 2.0.7 python 3.5

推荐答案

作为 @ Daniel Roseman 说,您可以将文件流直接用作,

As @ Daniel Roseman said, you can directly use the filestream as ,

def post(self, request):
    form = CsvUploadForm(request.POST, request.FILES)

    if form.is_valid():
        reader = csv.DictReader(request.FILES['csv'])
        ....
        ....

    return HttpResponse("file Uploaded")

这篇关于获取TypeError:预期的str,字节或os.PathLike对象,而不是InMemoryUploadedFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆