Error in finding files at a remote ftb server

this code trying to download files containing a specific matching letters in their names, but it can not reach the files and skip the subdirectories:

import ftplib
import os

ftp = ftplib.FTP(‘ftp-access.aviso.altimetry.fr’)
ftp.login(‘mah.ghy@bhit.bu.edu.eg’, ‘iRQ100’)

remote_directory_path = ‘/geophysical-data-record/jason-2/sgdr_d/’
local_directory_path = r’D:\altimetry course\codes\v’
search_word = ‘JA2’

def download_files_with_word(ftp, remote_directory_path, local_directory_path, search_word):
try:
ftp.cwd(remote_directory_path)
print(f’Current remote directory: {ftp.pwd()}‘)
for item in ftp.nlst():
print(f’Current item: {item}’)
if item.startswith(‘d’):
directory_name = item.split()[-1]
subdirectory_path = os.path.join(remote_directory_path, directory_name)
subdirectory_local_path = os.path.join(local_directory_path, directory_name)
os.makedirs(subdirectory_local_path, exist_ok=True)
download_files_with_word(ftp, subdirectory_path, subdirectory_local_path, search_word)
elif search_word in item:
remote_file_path = os.path.join(remote_directory_path, item)
print(f’Downloading {item} from {remote_file_path} to {local_directory_path}‘)
local_file_path = os.path.join(local_directory_path, item)
with open(local_file_path, ‘wb’) as local_file:
ftp.retrbinary(‘RETR ’ + remote_file_path, local_file.write)
print(f’Successfully downloaded {item} from {remote_file_path}.’)
else:
print(f’Skipping {item} because it does not contain the search word.’)
# Print out the full path of each file that is being skipped
remote_file_path = os.path.join(remote_directory_path, item)
print(f’Skipping {remote_file_path}.‘)
except ftplib.error_perm as e:
print(f’Error while accessing {remote_directory_path}: {e}’)

    # Recursively call the download_files_with_word function for each subdirectory
    for item in ftp.nlst():
        if item.startswith('d'):
            directory_name = item.split()[-1]
            subdirectory_path = os.path.join(remote_directory_path, directory_name)
            subdirectory_local_path = os.path.join(local_directory_path, directory_name)
            download_files_with_word(ftp, subdirectory_path, subdirectory_local_path, search_word)

download_files_with_word(ftp, remote_directory_path, local_directory_path, search_word)

ftp.quit()

Please put all the code pre-formatted block.


import ftplib
import os

ftp = ftplib.FTP('ftp-access.aviso.altimetry.fr')
ftp.login('mah.ghy@bhit.bu.edu.eg', 'iRQ100')

remote_directory_path = '/geophysical-data-record/jason-2/sgdr_d/'
local_directory_path = r'D:\altimetry course\codes\v'
search_word = 'JA2'

def download_files_with_word(ftp, remote_directory_path, local_directory_path, search_word):
    try:
        ftp.cwd(remote_directory_path)
        print(f'Current remote directory: {ftp.pwd()}')
        for item in ftp.nlst():
            print(f'Current item: {item}')
            if item.startswith('d'):
                directory_name = item.split()[-1]
                subdirectory_path = os.path.join(remote_directory_path, directory_name)
                subdirectory_local_path = os.path.join(local_directory_path, directory_name)
                os.makedirs(subdirectory_local_path, exist_ok=True)
                download_files_with_word(ftp, subdirectory_path, subdirectory_local_path, search_word)
            elif search_word in item:
                remote_file_path = os.path.join(remote_directory_path, item)
                print(f'Downloading {item} from {remote_file_path} to {local_directory_path}')
                local_file_path = os.path.join(local_directory_path, item)
                with open(local_file_path, 'wb') as local_file:
                    ftp.retrbinary('RETR ' + remote_file_path, local_file.write)
                print(f'Successfully downloaded {item} from {remote_file_path}.')
            else:
                print(f'Skipping {item} because it does not contain the search word.')
                # Print out the full path of each file that is being skipped
                remote_file_path = os.path.join(remote_directory_path, item)
                print(f'Skipping {remote_file_path}.')
    except ftplib.error_perm as e:
        print(f'Error while accessing {remote_directory_path}: {e}')

    # Recursively call the download_files_with_word function for each subdirectory
    for item in ftp.nlst():
        if item.startswith('d'):
            directory_name = item.split()[-1]
            subdirectory_path = os.path.join(remote_directory_path, directory_name)
            subdirectory_local_path = os.path.join(local_directory_path, directory_name)
            download_files_with_word(ftp, subdirectory_path, subdirectory_local_path, search_word)

download_files_with_word(ftp, remote_directory_path, local_directory_path, search_word)

ftp.quit()

It would help if you could show some of the output that it printed (ideally in a pre-formatted block for clarity).

Current item: cycle_019
Skipping cycle_019 because it does not contain the search word.
Skipping /geophysical-data-record/jason-2/sgdr_d/cycle_019.
Current item: cycle_293
Skipping cycle_293 because it does not contain the search word.
Skipping /geophysical-data-record/jason-2/sgdr_d/cycle_293.
Current item: cycle_643
Skipping cycle_643 because it does not contain the search word.
Skipping /geophysical-data-record/jason-2/sgdr_d/cycle_643.
Current item: cycle_092
Skipping cycle_092 because it does not contain the search word.
Skipping /geophysical-data-record/jason-2/sgdr_d/cycle_092.
Current item: cycle_263
Skipping cycle_263 because it does not contain the search word.
Skipping /geophysical-data-record/jason-2/sgdr_d/cycle_263.
Current item: cycle_001
Skipping cycle_001 because it does not contain the search word.
Skipping /geophysical-data-record/jason-2/sgdr_d/cycle_001.

*** As shown it skipped all subdirectories although I am sure they containing the files. I think the coded only search the main directory and can not search sub*  as I tried the same code and get the file from one directory.

Look at your code. It’ll search a subdirectory only if item starts with ‘d’. None of them start with ‘d’; they all start with ‘cycle_’.

OMG I missed it , thank you