I, Wilson

ChatGptGeneratedFtpCode

ChatGptGeneratedFtpCode

(This is going to be edited. The code formatted and such.

So I played around with the bing chatgpt instance a little bit and it did a lot of interesting work. But, in the end the mirroring code didn't work.

This shit is getting crazy: ChatGptGeneratedFtpCode



"Write me a python function that will upload a directory tree using ftp"

import os
from ftplib import FTP

def upload_directory_tree(ftp: FTP, local_dir: str, remote_dir: str):
"""
Uploads a local directory tree to a remote FTP server.

:param ftp: An authenticated FTP connection object.
:param local_dir: The path to the local directory to upload.
:param remote_dir: The path to the remote directory to upload to.
"""
for item in os.listdir(local_dir):
local_path = os.path.join(local_dir, item)
remote_path = f"{remote_dir}/{item}"
if os.path.isfile(local_path):
with open(local_path, "rb") as f:
ftp.storbinary(f"STOR {remote_path}", f)
elif os.path.isdir(local_path):
try:
ftp.mkd(remote_path)
except:
pass
upload_directory_tree(ftp, local_path, remote_path)
Copy
This function takes an authenticated FTP connection object, the path to the local directory to upload and the path to the remote directory to upload to. It recursively uploads all files and directories within the local directory tree to the remote FTP server.

Is there anything else you would like me to add or explain?


----

> Write me a python function that will upload only new or changed files from a directory tree using ftp

import ftplib
import os

def upload_new_or_changed_files(ftp_host, ftp_user, ftp_password, local_dir, remote_dir):
ftp = ftplib.FTP(ftp_host)
ftp.login(ftp_user, ftp_password)
for root, dirs, files in os.walk(local_dir):
for file in files:
local_file_path = os.path.join(root, file)
remote_file_path = os.path.join(remote_dir, os.path.relpath(local_file_path, local_dir))
try:
ftp.cwd(os.path.dirname(remote_file_path))
except ftplib.error_perm:
ftp.mkd(os.path.dirname(remote_file_path))
ftp.cwd(os.path.dirname(remote_file_path))
with open(local_file_path, 'rb') as f:
try:
ftp.size(remote_file_path)
print(f"{remote_file_path} already exists. Skipping.")
except ftplib.error_perm:
print(f"Uploading {remote_file_path}")
ftp.storbinary(f"STOR {remote_file_path}", f)
ftp.quit()

----

> How can I modify this function to delete files on the remote server that are not present in the local directory?

import ftplib
import os

def mirror_local_directory(ftp_host, ftp_user, ftp_password, local_dir, remote_dir):
ftp = ftplib.FTP(ftp_host)
ftp.login(ftp_user, ftp_password)

# Get a list of all files on the remote server
remote_files = []
ftp.retrlines('LIST', remote_files.append)
remote_files = [f.split()[-1] for f in remote_files]

# Create directories and upload files
for root, dirs, files in os.walk(local_dir):
for dir in dirs:
local_dir_path = os.path.join(root, dir)
remote_dir_path = os.path.join(remote_dir, os.path.relpath(local_dir_path, local_dir))
try:
ftp.cwd(remote_dir_path)
except ftplib.error_perm:
ftp.mkd(remote_dir_path)
for file in files:
local_file_path = os.path.join(root, file)
remote_file_path = os.path.join(remote_dir, os.path.relpath(local_file_path, local_dir))
try:
ftp.cwd(os.path.dirname(remote_file_path))
except ftplib.error_perm:
ftp.mkd(os.path.dirname(remote_file_path))
ftp.cwd(os.path.dirname(remote_file_path))
with open(local_file_path, 'rb') as f:
try:
ftp.size(remote_file_path)
print(f"{remote_file_path} already exists. Skipping.")
except ftplib.error_perm:
print(f"Uploading {remote_file_path}")
ftp.storbinary(f"STOR {remote_file_path}", f)
if remote_file_path in remote_files:
remote_files.remove(remote_file_path)

# Delete any remaining files on the remote server
for file in remote_files:
print(f"Deleting {file}")
ftp.delete(file)

ftp.quit()

mirror_local_directory('ftp.example.com', 'username', 'password', '/path/to/local/dir', '/path/to/remote/dir')

----





----
PostMe
CategoryProgramming
CategoryAi
CategoryChatGpt

This is going to be a block of interesting starting points. But right now it's an exhaustive list of every single page (sans left-column date pages) on the site. Baby steps and all.