Uploading multiple files in a single request using python requests module

前端 未结 6 604
南笙
南笙 2020-12-04 19:49

The Python requests module provides good documentation on how to upload a single file in a single request:

 files = {\         


        
相关标签:
6条回答
  • 2020-12-04 20:14

    To upload a list of files with the same key value in a single request, you can create a list of tuples with the first item in each tuple as the key value and the file object as the second:

    files = [('file', open('report.xls', 'rb')), ('file', open('report2.xls', 'rb'))]
    
    0 讨论(0)
  • 2020-12-04 20:16

    I'm a little bit confused, but directly opening file in request (however same is written in official requests guide) is not so "safe".

    Just try:

    import os
    import requests
    file_path = "/home/user_folder/somefile.txt"
    files = {'somefile': open(file_path, 'rb')}
    r = requests.post('http://httpbin.org/post', files=files)
    

    Yes, all will be ok, but:

    os.rename(file_path, file_path)
    

    And you will get:

    PermissionError:The process cannot access the file because it is being used by another process
    

    Please, correct me if I'm not right, but it seems that file is still opened and I do not know any way to close it.

    Instead of this I use:

    import os
    import requests
    #let it be folder with files to upload
    folder = "/home/user_folder/"
    #dict for files
    upload_list = []
    for files in os.listdir(folder):
        with open("{folder}{name}".format(folder=folder, name=files), "rb") as data:
            upload_list.append(files, data.read())
    r = request.post("https://httpbin.org/post", files=upload_list)
    #trying to rename uploaded files now
    for files in os.listdir(folder):
        os.rename("{folder}{name}".format(folder=folder, name=files), "{folder}{name}".format(folder=folder, name=files))
    

    Now we do not get errors, so I recommend to use this way to upload multiple files or you could receive some errors. Hope this answer well help somebody and save priceless time.

    0 讨论(0)
  • The documentation contains a clear answer.

    Quoted:

    You can send multiple files in one request. For example, suppose you want to upload image files to an HTML form with a multiple file field ‘images’:

    To do that, just set files to a list of tuples of (form_field_name, file_info):

    url = 'http://httpbin.org/post'
    multiple_files = [('images', ('foo.png', open('foo.png', 'rb'), 'image/png')),
                          ('images', ('bar.png', open('bar.png', 'rb'), 'image/png'))]
    r = requests.post(url, files=multiple_files)
    r.text
    
    # {
    #  ...
    #  'files': {'images': 'data:image/png;base64,iVBORw ....'}
    #  'Content-Type': 'multipart/form-data; boundary=3131623adb2043caaeb5538cc7aa0b3a',
    #  ...
    # }
    
    0 讨论(0)
  • 2020-12-04 20:20

    If you have multiple files in a python list, you can use eval() in a comprehension to loop over the files in the requests post files parameter.

    file_list = ['001.jpg', '002.jpg', '003.jpg']
    files=[eval(f'("inline", open("{file}", "rb"))') for file in file_list ]
    
    requests.post(
            url=url,
            files=files
    )
    
    0 讨论(0)
  • 2020-12-04 20:22

    Multiple files with different key values can be uploaded by adding multiple dictionary entries:

    files = {'file1': open('report.xls', 'rb'), 'file2': open('otherthing.txt', 'rb')}
    r = requests.post('http://httpbin.org/post', files=files)
    
    0 讨论(0)
  • 2020-12-04 20:23

    You need to create a file list to upload multiple images:

    file_list = [  
           ('Key_here', ('file_name1.jpg', open('file_path1.jpg', 'rb'), 'image/png')),
           ('key_here', ('file_name2.jpg', open('file_path2.jpg', 'rb'), 'image/png'))
       ]
    
    r = requests.post(url, files=file_list)
    

    If you want to send files on the same key you need to keep the key same for each element, and for a different key just change the keys.

    Source : https://stackabuse.com/the-python-requests-module/

    0 讨论(0)
提交回复
热议问题