Register Login

How to Upload Files in Python?

File uploading is an essential utility every developer should know. Various software applications require uploading files to do certain operations. Several desktops and web applications demand uploading files from the client or the users in the form of a regular file (e.g., an audio file, an image file, text file, etc.) Most uploading of files refers to applications that use the internet.

Python supports different libraries that simplify data transfer over HTTP. There are three different packages that developers can use to upload a file in a python program.

Uploading Files through a Python program:

There are various ways of uploading files through a python program. Some might require the support of HTML script and buttons while others can directly upload files through the program itself.

Method 1: Using the Python’s os Module:

In this method, we have to use the HTML code to set specific actions. HTML caters to a variety of attributes for different HTML elements. We have to use the <form> along with the action attribute to set the Python program for execution. Also, the enctype attribute with "multi-part/form-data" value will help the HTML form to upload a file. Lastly, we need the input tag with the filename attribute to upload the file we want.

Here is our HTML code:

<html>
<body>
Uploading file by executing a Python script
<form enctype = "multipart/form-data" action = "upload_script.py" method = "post">
<br> File Uploading <input type = "file" name = "filename" />
<p> <input type = "submit" value = "Upload Now" /> </p>
</form>
</body>
</html>

Lastly, we need the input tag with the filename attribute to upload the file we want. The os module in Python allows the Python program to interact with the system. As our file resides as a part of our operating system, we need to use the os module in our Python program.

Python code [upload_script.py]:

import os
fi = form['filename']
if fi.filename:
	# This code will strip the leading absolute path from your file-name
	fil = os.path.basename(fi.filename)
	# open for reading & writing the file into the server
	open(fn, 'wb').write(fi.file.read())

Explanation:

Here, we have first imported the OS module so that we can deal with the operating system related operations. Next, we have to create an identifier that will hold the filename for uploading. Now, using the if condition, we have to check whether the file name exists or not.

If yes, we will use the os.path.basename() to extract the filename by striping the leading absolute path from the file. We will then use another identifier to store that path. Now, we can open the file for reading and writing it into the server.

Method 2: Using the Requests library:

The requests module contains many predefined methods that allow developers to send HTTP requests using Python. The HTTP request delivers a response object containing response data such as encoding, content, status, etc. Using this, you do not need to manually append query strings for the URLs or any other form-encoding for the PUT & POST data. Since it is not a built-in library, we have to install it using the pip.

$ pip install requests

Now, you can create the Python file and import the requests into your program.

import requests
dfile = open("datafile.txt", "rb")
url = "http://httpbin.org/post"
test_res = requests.post(url, files = {"form_field_name": dfile})
if test_res.ok:
    print(" File uploaded successfully ! ")
    print(test_res.text)
else:
    print(" Please Upload again ! ")

Explanation:

This technique uses the requests library and for using it, we have to import it in our Python program. Then, we will open our file (datafile.txt) in read binary mode. Next, define a string identifier that stores the URL. Then, we have to call the post() method and pass the URL and the opened file (as a Python dictionary).

Now, we will check whether test_res (test result) is Ok or not. If It is OK, then we will print a success message along with the resultant text. Otherwise, we will prompt the user to upload it again.

Method 3: Using Filestack API:

We can also use the Python SDK and call the filestack API (Application Programming Interface) to upload files through the Python program. To obtain this SDK, you need to install it using the PIP command.

pip install filestack-python

Once you have installed the filestack SDK, you have to begin it with your Python program. Then you have to create an instance of the Client using the Filestack API key. This Client will perform the heavy operations for you in this program.

Program

from filestack import Client
c = Client("API's Key")
filelnk = c.upload(filepath = '/path/of/file.png')
print(filelnk.url)

Make sure you replace the "API's Key" with the actual API key you generate before writing the program.

Explanation:

Filestack API requires importing in our Python Program. Once we import the Client module from the filestack, we will provide the API key (the one we will receive at the time of registration). Store it in a separate variable. Next, connect to the file link that you want to upload and set the file path as athe argument value in the upload() method. Finally, display the filelnk file.

Uploading Multiple Files in Python:

Now, since you have got a basic understanding of how to deal with uploading a single file in Python, let us now move to a new topic – uploading multiple files in Python. Here, we will use the Python script to fetch the file from your system. In this section, you will use the requests library.

Program:

import requests
testUrl = "http://httpbin.org/post"
testFiles = {
    "test_file_1": open("file1.txt", "rb"),
    "test_file_2": open("file2.txt", "rb"),
    "test_file_3": open("file3.txt", "rb")
}

responseVar = requests.post(testUrl, files = testFiles)
if responseVar.ok:
    print("Successfully Uploaded all files !")
    print(responseVar.text)
else:
    print("Upload failed !")

Output:

Explanation:

Here, we will first import the requests module. Then, we will use the testUrl variable to put the post HTTP request. Then we will create a dictionary by the name testFiles that will have three key-value pairs where the keys will be the file-id and values are the file names.

Then we will execute the post request method that will have two parameters, the testUrl and the files that will hold multiple filenames and store the entire return value in the responseVar. Then, we will check whether the responseVar is executing smoothly or not.

If yes, then a success message will be displayed using the print() along with the responseVar.text(). Otherwise, it will return an error or failure message using the print(). In this technique, you simply have to name the files in the dictionary value to upload them all at once.

Conclusion:

Among all these methods, API calls (third method) take the maximum time and hence the least efficient. The first technique is explicitly used in web application development and the second technique is used in desktop or stand-alone application development. The OS module is faster as compared to the requests library because it uses frequent system calls and is closer to machine. But for file uploading purpose, the request module is easy to use.