How To Upload Multiple Files In Python

One common task that Python developers have to encounter is managing and uploading multiple files at once. However, doing this efficiently not only saves time but also ensures optimal use of resources.

In this tutorial, we will discuss how to upload multiple files in Python using os and shutil modules. This tutorial assumes you have a basic understanding of Python programming.

Step 1: Import the Necessary Modules

To get started, we need to import the necessary modules. We will be using these modules to help us manage and move files in our script. Add the following lines to your script:

Step 2: Set the Source and Destination Folders

First, define the source and destination folders. The source folder is where your files are currently located, and the destination folder is where you want to move the files. Replace 'source_folder' and 'destination_folder' with the actual paths to the respective directories.

Step 3: Get a List of Files in the Source Folder

To list all files in the source folder, we can use the os.listdir() function. This function returns a list of filenames as strings. Loop through this list to work with each file individually.

Step 4: Filter the Files Based on Your Criteria (Optional)

In some cases, you may want to upload only specific files and not all files from the source folder. You can filter the list of files based on extensions, size, or other criteria. For example, let’s filter out only the .txt files.

Step 5: Upload the Files

Finally, iterate through the filtered files and use the shutil.move() function to move each file to the destination folder. The shutil.move() function takes two arguments: the source file path and the destination file path.

At this point, your script should successfully upload multiple files from the source directory to the destination directory.

Here is the full code for this tutorial:


The .txt files from the 'source_folder' directory have been uploaded to the 'destination_folder' directory.


In this tutorial, we learned how to upload multiple files in Python using the os and shutil modules. By defining different filtering criteria, you can select and upload only specific files from the source directory to the destination directory. This process can be easily extended and customized to cater to different use cases and file management tasks.