Python: Upload Multiple Files with Requests Library – A Beginner Example

In this tutorial, we will introduce how to upload multiple files using python requests library.

Python: Upload Multiple Files with Requests Library - A Beginner Example

1.Install requests library

pip install requests

2.Import library

import requests

3.Prepare a url to receive files

test_url = "http://httpbin.org/post"

4.Prepare files you plan to upload

test_files = {
    "test_file_1": open("my_file.txt", "rb"),
    "test_file_2": open("my_file_2.txt", "rb"),
    "test_file_3": open("my_file_3.txt", "rb")
}

5.Start to upload files

test_response = requests.post(test_url, files = test_files)

if test_response.ok:
    print("Upload completed successfully!")
    print(test_response.text)
else:
    print("Something went wrong!")

In this example code, we will use requests.post() to upload files.