Python script to upload files to  a server

Python script to upload files to a server

Table of contents

No heading

No headings in the article.

FTP (File Transfer Protocol) is a widely used network protocol that allows you to transfer files between computers on a network. It provides a way to upload, download, and manage files on a remote server.

When uploading files via FTP, the process typically involves the following steps:

  1. Establishing a connection: To get started, the client establishes a connection to the FTP server using the server's hostname, username, and password. This connection serves as a communication channel for transferring files.

  2. Navigating directories: Once connected, the client can explore the directory structure on the server. This involves moving through different directories using commands like "change working directory" and "print working directory." These commands help the client locate the desired destination for file upload.

  3. Uploading files: To upload a file, the client uses the "store" command (STOR) along with the specified file name. Depending on the file type, it can be uploaded in either binary mode or text mode. Binary mode is suitable for non-text files like images or executables, while text mode is used for plain text files.

  4. Transferring file data: The client opens the local file, reads its contents, and sends the data over the FTP connection to the server. The server then receives the data and stores it as the specified file on its file system. This process ensures that the file is successfully uploaded to the server.

  5. Closing the connection: After the file transfer is complete, the client can gracefully close the connection to the FTP server. This is typically done using the "quit" command or by calling the appropriate method provided by the FTP library being used.

Below is a script implementing the steps:

import os
import ftplib

username = "dlpuser"
pwd = "rNrKYTX9g7z3RgJRmxWuGHbeu"
remote_dir = 'images'
local_dir = 'C:/Users/USER/OneDrive/Images/PINTEREST/'
history_file = 'history.txt'

ftp = ftplib.FTP("ftp.dlptest.com")
ftp.login(username, pwd)

ftp.cwd(remote_dir)

# Create an empty history file if it doesn't exist
if not os.path.exists(history_file):
    with open(history_file, 'w') as file:
        # Write an initial message or leave it blank
        file.write('This is the history file for uploaded files.\n')

# Read the previously uploaded files from the history file
uploaded_files = []
if os.path.exists(history_file):
    with open(history_file, 'r') as file:
        uploaded_files = file.read().splitlines()

# Get the list of files in the local directory
files = os.listdir(local_dir)

# Compare and upload only the new files
for file in files:
    if file.endswith('.jpg') and file not in uploaded_files:
        local_filepath = os.path.join(local_dir, file)

        with open(local_filepath, 'rb') as local_file:
            ftp.storbinary('STOR %s' % file, local_file)

        print('Uploaded: %s' % file)
    else:
        print("Failed to upload: %s" % file)

# Update the history file with the new uploads
with open(history_file, 'a') as file:
    file.write('\n'.join(files))

ftp.quit()

Line by line explanation of whats happening:

  1. We start by setting up the necessary credentials, including the FTP server's username, password, and the remote and local directories involved.

  2. We establish a connection to the FTP server using the provided credentials.

  3. If a history file (e.g., "history.txt") doesn't exist, we create one to keep track of the uploaded files. We write an initial message in the file.

  4. We read the contents of the history file, which contains a list of previously uploaded files.

  5. We retrieve the list of files in the local directory.

  6. For each file in the local directory, we check if it ends with ".jpg" and if it is not in the list of uploaded files.

  7. If the file meets the criteria, we open it in binary mode and upload it to the FTP server using the storbinary function.

  8. We print a message indicating whether the upload was successful or failed for each file.

  9. We update the history file by appending the names of the uploaded files to it.

  10. Finally, we close the FTP connection.

By maintaining a history file, the code ensures that only new files are uploaded to the FTP server, preventing duplicate uploads. Additionally, in case the upload process is interrupted, the code can be rerun, and it will resume from where it left off, as it checks the history file to determine which files have already been uploaded.

FTP can be accessed using various FTP client software or command-line tools. Additionally, programming languages like Python provide FTP libraries, such as ftplib, which allow you to write code for performing FTP file transfers.

When working with FTP uploads in your code, it's essential to handle potential errors, ensure successful transfers, and implement appropriate error handling mechanisms to maintain the reliability of the file upload process.

Happy coding!