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:
1 2 |
import os import shutil |
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.
1 2 |
source_folder = 'source_folder' destination_folder = 'destination_folder' |
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.
1 |
files = os.listdir(source_folder) |
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.
1 |
filtered_files = [file for file in files if file.endswith('.txt')] |
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.
1 2 3 4 |
for file in filtered_files: src_file_path = os.path.join(source_folder, file) dst_file_path = os.path.join(destination_folder, file) shutil.move(src_file_path, dst_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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import os import shutil source_folder = 'source_folder' destination_folder = 'destination_folder' files = os.listdir(source_folder) filtered_files = [file for file in files if file.endswith('.txt')] for file in filtered_files: src_file_path = os.path.join(source_folder, file) dst_file_path = os.path.join(destination_folder, file) shutil.move(src_file_path, dst_file_path) |
Output
The .txt files from the 'source_folder' directory have been uploaded to the 'destination_folder' directory.
Conclusion
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.