10

Python fails while uploading a file which its size bigger than 8192 bytes. And the exception is only "got more than 8192 bytes". Is there a solution to upload larger files.

try:
    ftp = ftplib.FTP(str_ftp_server )
    ftp.login(str_ftp_user, str_ftp_pass)
except Exception as e:
    print('Connecting ftp server failed')
    return False

try:
    print('Uploading file ' + str_param_filename)
    file_for_ftp_upload = open(str_param_filename, 'r')
    ftp.storlines('STOR ' + str_param_filename, file_for_ftp_upload)

    ftp.close()
    file_for_ftp_upload.close()
    print('File upload is successful.')
except Exception as e:
    print('File upload failed !!!exception is here!!!')
    print(e.args)
    return False

return True
1
  • Does the file contain text? If not, you probably want to open in binary mode and use ftp.storbinary().
    – augurar
    Commented Jan 19, 2015 at 0:47

2 Answers 2

12

storlines reads a text file one line at a time, and 8192 is the maximum size of each line. You're probably better off using, as the heart of your upload function:

with open(str_param_filename, 'rb') as ftpup:
    ftp.storbinary('STOR ' + str_param_filename, ftpup)
    ftp.close()

This reads and stores in binary, one block at a time (same default of 8192), but should work fine for files of any size.

2
  • Careful with this solution if sending and receiving systems are of different types. Sending as binary will not automatically translate character encoding etc. between different types of systems. Binary and text modes have could have different behavior.
    – stenix
    Commented Sep 10, 2015 at 12:39
  • @stenix, if you don't want to actually upload the file as it is (which is exactly all the OP's request to "upload a file" implied!), but rather need to apply any kind of translation, then of course you need very different code than for just uploading -- widely varying depending on WHAT translation you need, of course. Commented Sep 10, 2015 at 17:42
4

I had a similar issue and solved it by increasing the value of ftplib's maxline variable. You can set it to any integer value you wish. It represents the maximum number of characters per line in your file. This affects uploading and downloading.

I would recommend using ftp.storbinary in most cases per Alex Martelli's answer, but that was not an option in my case (not the norm).

ftplib.FTP.maxline = 16384    # This is double the default value

Just call that line at any point before you start the file transfer.

1
  • This worked perfectly. Thanks a lot. This worked for retrlines() as well. Commented Mar 11, 2019 at 14:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.