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)
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.