Such a problem ... When downloading files from an FTP server, some of them are loaded empty (not all). Please tell me what could be the problem?

Here is a piece of code ...

ftp_host = '' ftp_user = '' ftp_password = '' ftp_connect = FTP(ftp_host, ftp_user, ftp_password) ftp_connect.cwd('IN') file_names = ftp_connect.nlst() for filename in file_names: host_file = os.path.join('c:/ftp_test/', filename) try: with open(host_file, 'wb') as local_file: print('Copy... ' + filename) ftp_connect.retrbinary('RETR ' + filename, local_file.write) except ftplib.error_perm: pass 
  • Maybe it's in the maxblocksize parameter of the retrbinary method? - Arthur Dzhemakulov

1 answer 1

nlst returns both file names and directories. Probably empty local files are ftp directories. Those need to do something like this:

 folders_and_files = ftp_connect.nlst() file_names = [name for name in folders_and_files if (not ftp_connect.cwd(name))] for filename in file_names: ... 

In general, the answer to your question except ftplib.error_perm When this error occurs, the open(host_file, 'wb') file open(host_file, 'wb') has already been created, but because of the error, nothing has been downloaded, the file is empty.

  • 100% on FTP in this folder there are no directories. I manually compare files - on FTP, say, 410 kilobytes, and locally - 0. Thanks for the information, I will take note. - Arthur Dzhemakulov