Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以python django的名称上载的文件_Python_Django_File Upload - Fatal编程技术网

以python django的名称上载的文件

以python django的名称上载的文件,python,django,file-upload,Python,Django,File Upload,我使用的代码是: def saveUploadedInventory(self, inventory_file,user_id): with open('uploaded_inventory_sheet.csv','wb+') as destination: for chunk in inventory_file.chunks(): destination.write(chunk) reader = csv.read

我使用的代码是:

  def saveUploadedInventory(self, inventory_file,user_id): 
      with open('uploaded_inventory_sheet.csv','wb+') as destination:
          for chunk in inventory_file.chunks():
              destination.write(chunk)

      reader = csv.reader(open('uploaded_inventory_sheet.csv','rb'))
已成功上载名为Upload\u inventory\u sheet.csv的文件

但我想上传同一个文件在不同的目录,其实际名称是来自客户端

我尝试以下代码:

def saveUploadedInventory(self, inventory_file,user_id): 
    with open(''.join(inventory_file),'wb+') as destination:
        for chunk in inventory_file.chunks():
            destination.write(chunk)
    reader = csv.reader(open('inventory_file','rb'))
但它给出了以下错误:

异常类型:IOError

异常值:[Errno 36]文件名也是 龙:嗯


要打开文件名,而不是加入其内容:

def saveUploadedInventory(self, inventory_file,user_id): 
    with open(inventory_file.name,'wb+') as destination:
        for chunk in inventory_file.chunks():
            destination.write(chunk)
    reader = csv.reader(open(inventory_file.name,'rb'))
open
采用您希望打开的文件的名称,因此您应该使用
inventory\u file
name
字段中提供的名称

发件人:

UploadedFile.name

The name of the uploaded file (e.g. my_file.txt).

“”的输出是什么?联接(库存文件)?它看起来很长,而你真正想要的只是名称。我得到错误
文件名太长了
,然后文件的所有数据都被打印在上面,以及如何将该文件写入不同的目录而不是project foderTo写入不同的文件夹
并打开(/path/to/inventory\u file.name,'wb+'))作为目的地